content
The selenium test tool can be used to simulate the operation of the user's browser. The supported browsers are: PhantomJS, Firefox, Chrome, etc. The developer can choose different simulated browsers according to the current system form.
Each simulated browser needs to correspond to The browser driver (an executable file suffixed with .exe), the author uses Google Chrome, the corresponding browser driver can be downloaded from the following URL
- Google official (generally inaccessible due to firewall protection)
- mirror site
Browser version and driver version
1. View the browser version
Open Google Chrome >>>Settings>>>About Chrome>>>View Browser Version
2. Driver version selection and download
Open the mirror website above, we can see a variety of different versions of the Chrome driver,
select the driver version closest to the browser version, click the blue link
Each blue link corresponds to the version of the driver, which is divided into different compressed packages due to different systems.
The author's here only introduces the driver download of windows, so we click win32.zip
the downloaded compressed package and save it on the desktop
After opening, we see that there is only one .exe file inside, which is what we call the Chrome browser driver
Chromedriver configuration
We have downloaded the browser driver above, but it can't be used directly in the program, otherwise the error below the program will be
'selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. '
Regarding its configuration, there are usually the following methods
1. Using the executable_path parameter
When selenium.webdriver.Chrome()
instantiating a driver, you need to executable_path
specify the path of the driver through parameters
Example: use Baidu to automatically search for python
from selenium import webdriver
import time
url='http://www.baidu.com'
path='e:\\chromedriver.exe'
driver = webdriver.Chrome(executable_path=path)
driver.get(url)
driver.find_element('id','kw').send_keys('python')
driver.find_element('id','su').click()
time.sleep(2)
driver.quit()
operation result
2. Create a Service object
Recently using the first method program raised the following warning: 'DeprecationWarning: executable_path has been deprecated, please pass in a Service object'
The program is telling us that executable_path has been deprecated, please pass in a Service object, maybe because the selenium library has been updated, we can check it firstwebdriver.py
From the above, we can see the reason for the program error. Sure enough, this parameter used to represent the executable file path has been deprecated, but it seems that it is stored in a service object. Let's take a lookservice.py
Now that executable_path is refactored into service.py, we can instantiate a Service object to represent the browser-driven path
from selenium import webdriver
import time
url='http://www.baidu.com'
path=Service('e:\\chromedriver.exe') # 将路径实例化为一个Service对象
driver = webdriver.Chrome(service=path)
driver.get(url)
driver.find_element('id','kw').send_keys('python')
driver.find_element('id','su').click()
time.sleep(2)
driver.quit()
Results of the
3. Use default values
After downloading the .exe file, we transfer it to the python interpreter: python.exe in Scripts
the same level directory
from selenium import webdriver
import time
url='http://www.baidu.com'
driver = webdriver.Chrome()
driver.get(url)
driver.find_element('id','kw').send_keys('python')
driver.find_element('id','su').click()
time.sleep(2)
driver.quit()
operation result