About selenium configuration Chrome driver (Windows system)


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

  1. Google official (generally inaccessible due to firewall protection)
  2. mirror site

Browser version and driver version

1. View the browser version

Open Google Chrome >>>Settings>>>About Chrome>>>View Browser Version
insert image description here

2. Driver version selection and download

Open the mirror website above, we can see a variety of different versions of the Chrome driver, insert image description here
select the driver version closest to the browser version, click the blue link
insert image description here

Each blue link corresponds to the version of the driver, which is divided into different compressed packages due to different systems.insert image description here

The author's here only introduces the driver download of windows, so we click win32.zipthe downloaded compressed package and save it on the desktop
insert image description here

After opening, we see that there is only one .exe file inside, which is what we call the Chrome browser driver

insert image description here

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. '
insert image description here

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_pathspecify 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
insert image description here

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'
insert image description here

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
insert image description here

insert image description here
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

insert image description here
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
insert image description here

3. Use default values

After downloading the .exe file, we transfer it to the python interpreter: python.exe in Scriptsthe same level directory
insert image description here

insert image description here

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
insert image description here

Guess you like

Origin blog.csdn.net/m0_54510474/article/details/121278693