Browser version and driver matching for Selenium2 automated testing

Browser version and driver matching for Selenium2 automated testing

This blog post is intended for beginners to learn Selenium, the browser version does not match the driver version, which will result in the failure to execute automated test cases:

  • Selenium2 automated testing environment to build
  • The pit of matching browser version and browser driver version

Selenium2 automated test environment construction

Install Python

It is recommended that you use Python3, but selenium does not enforce the version of Python.

In the Windows environment, you can directly download the file with the suffix ".msi" from the Python official website, and double-click to install it. During the installation process, you can choose to add Python to the environment variables to save the process of manually configuring the environment variables. After the installation is complete, press win + R, enter cmd to enter the windows command prompt, enter the "python" command, if the following interface is displayed, the installation is successful:
write picture description here
If it prompts "python is not an internal command", don't panic, it means the python environment variable No, you can change it manually.

If you installed Python3, which has integrated pip, you can go to the Script folder in the Python installation directory to check whether there is a pip.exe or pip3.exe file. If not, you can download it from https://pypi.Python.org/pypi/pip .
If so, you can directly enter the pip or pip3 command in the Windows command line:
write picture description here
If the above pip command description information appears, it means that pip has been successfully installed. If it prompts "pip is not an internal command", don't panic, we can add: Python directory\Scripts\ path to the path of the system environment variable.

Before installing Selenium
, we have installed pip, now use the pip command to install Selenium:
write picture description here

After installing selenium, you need to install the matching version of the browser and browser driver. After downloading the browser driver, you can put the driver in:
Python directory\Scripts\ path, because we have added this path to the system environment variable before.

If the version does not match, it will enter a big pit. Let's take a look at a small demo:

#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("http://www.baidu.com")

driver.find_element_by_id("kw").send_keys("it works c72")
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'a')
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'x')
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'v')

driver.find_element_by_id("su").click()

print(driver.current_url)
driver.quit()

The result of the operation will report an error:
write picture description here

At this point the automation use case code can run, but after automatically opening Google Chrome, the send_keys code snippet will not execute.
After the author changes and installs the v58 version of Google Chrome, it can correspond to the v2.31 version driver.
All you need to do is install the corresponding version of the driver.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324854480&siteId=291194637