Selenium3 + Python3 a series of automated tests - install Python + selenium and selenium3 browser driver

First, install Python

  https://www.python.org/downloads/

  Python verify whether the installation was successful. Open the Windows command prompt (cmd), enter python, carriage return

  Note: During the installation need to check: "Add Python 3.x to PATH", if not checked, it is necessary after the installation is complete, the Python installation directory (eg: C: \ Python37) added to the PATH environment variable below.

Second, install selenium

1, a first installation method: Pycharm installation Selenium, command: pip install selenium

2, the second installation method: Locate Pycharm in the File -> Settings -> Project -> Project Interpreter, click on the + sign in the upper right corner, search selenium, click on the bottom Install Package to install selenium.

3, selenium verify whether the installation was successful

Third, the first selenium + python program

Above have been successfully download the corresponding version of the browser chromedriver drive. Here we have to write the first selenium + python program. I will describe two methods for reference study.

method one:

1, the downloaded chromedriver.exe (2.46) into (copied or moved) to the installation directory of chrome (chrome installation path generally follows, C: \ Program Files (x86) \ Google \ Chrome \ Application), as shown below below:

2, the test code is as follows:

from selenium.webdriver import Chrome
import time

driver = Chrome("C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
driver.get('https://www.baidu.com')
print(driver.title)
time.sleep(5) driver.quit()

方法二:

1、将下载的chromedriver.exe(2.46)放到(复制或移动)至Python的安装目录下,与python.exe文件相同目录下。查看Python的安装目录(cmd输入命令where python)。如下图所示:

2、测试代码如下:

from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
print(driver.title)
time.sleep(5) driver.quit()

两种方法运行结果一样,运行结果如下图所示:

 

注:如下载chromedriver版本不对或没有将chromedriver.exe文件拷贝至相应的目录下,则会出现:“selenium.common.exception.WebDriverException:Message:'chromedriver' executable needs to be in Path”报错。

 

当你运行demo程序,成功出现百度首页时恭喜你并打印出“百度一下,你就知道”,恭喜你,已经成功运行第一个selenium+python测试程序。

 

Guess you like

Origin www.cnblogs.com/wuweiblogs/p/11422933.html