02 测试环境的搭建

1.windows下环境搭建

(1)安装Python

访问Python 官方网站:https://www.Python.org/

我已经安装了Python2.7,不再重复安装。

(2)安装steuptools与pip

setuptools与pip下载地址:

https://pypi.Python.org/pypi/setuptools

https://pypi.Python.org/pypi/pip

已经配置过了,不再重复安装。

(3)安装Selenium

通过pip进行安装。

安装命令:pip install selenium

升级命令:pip install -U selenium

卸载命令:pip uninstall selenium

下载地址:https://pypi.python.org/pypi/selenium/

(4)ActivePython

下载地址:http://www.activestate.com/activePython/downloads

下载安装即可。

我使用的是Pycharm。

2.编写第一个自动化脚本—— baidu.py

# codiong=uft-8
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.baidu.com")

driver.find_element_by_id("kw").send_keys("Selenium2")
driver.find_element_by_id("su").Click()
driver.quit()

运行之前,需要进行以下配置:

下载安装FireFox浏览器,默认安装在了C盘,安装目录为:

C:Program Files (x86)Mozilla Firefox

2)geckodriver.exe

下载地址:https://github.com/mozilla/geckodriver/releases

请根据系统版本选择下载。

下载解压后将getckodriver.exe复制到Firefox的安装目录下。

在环境变量Path中添加路径:C:Program Files (x86)Mozilla Firefox,重启cmd或IDLE再次运行代码即可。

运行脚本,如下:

原文地址:https://www.cnblogs.com/Sumomo0516/p/6437381.html