About selenium configuring Chrome drivers (Windows systems)

Posted by 990805 on Thu, 11 Nov 2021 17:25:46 +0100


The selenium test tool can be used to simulate the operation of a user's browser. The supported browsers are PhantomJS,Firefox,Chrome, etc. Developers can select different simulated browsers according to the current system form.
Each simulated browser requires a corresponding browser driver (an executable file suffixed with.exe). The author uses Google Browser Chrome, which can be downloaded from the following website

  1. google official (usually inaccessible due to firewall protection)
  2. Mirror site

Browser Version and Driver Version

1. Browser Version View

Open Google Browser >> Settings > > About Chrome > View the browser version

2. Driver Version Selection and Download

Opening the mirror site above, we can see many different versions of Chrome drivers
Select the closest driver version to the browser version and click the blue link

Each blue link corresponds to the version driver, which is divided into different compression packages for different systems.

The author's description here is only for windows Driven Downloads, so we click to download the compressed package of win32.zip and save it on the desktop

When we open it, we see that there is only one.exe file inside, which is what we call the Chrome browser driver.

Configuration of Chromedriver

We've downloaded the browser driver above, but we can't use it directly in the program, otherwise the following error will occur
'selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. '

There are usually several ways to configure it

1. Use executable_path parameter

When instantiating a driver through selenium.webdriver.Chrome(), executable_is required The path parameter indicates the path of the drive

Example: Use Baidu to search python automatically

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()

Run Results

2. Create Service Objects

The following warning was recently raised using the first method program:'DeprecationWarning: executable_path has been deprecated, please pass in a Service object'

The program is telling us executable_path has been deprecated. Please pass in a Service object, probably because the selenium library has been updated. We can look at webdriver.py first.


From the above we can see the cause of the program error. If this parameter representing the path to the executable file has been discarded, but it looks like it is stored in a service object, let's take a look at service.py again.


Since executable_path is refactored into service.py, and we can instantiate a Service object to represent a browser-driven path

from selenium import webdriver
import time

url='http://www.baidu.com'
path=Service('e:\\chromedriver.exe')	# Instantiate the path into a Service object
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 enforcement

3. Use default values

When the.Exe file is downloaded, we move it to the python interpreter: python.exe sibling directory Scripts

Figure

Next, when we instantiate the driver object, we don't need to specify the path again

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()

Run Results

Topics: Python Selenium chrome