1、Selenium+Python 配置2目录Selenium+Python 配置 .1环境部署 .3安装程序 .3安装 python.3安装 selenium .3安装 setuptools .3安装 pip .3安装 selenium.3安装 webdriver.4安装 firefox webdirver .4安装 chrome webdriver.4安装 ie webdriver .4自定义模块 .4Demo .53环境部署安装程序python-2.7.2.msi,python 安装程序setuptools-0.6c11.win32-py2.7.exe,安装 selenium 必备软件pip
2、-1.0.2.tar.gzselenium-2.18.1.tar.gz(pip 命令下载安装) ,selenium 安装程序selenium-ide-1.6.0.xpi,firefoxWebDriverselenium-server-standalone-2.18.0.jar,ieWebDriverchromedriver.exe(chromedriver_win_18.0.1022.0.zip ) ,chromeWebDriverufida.zip安装 python双击安装 python-2.7.2.msi 即可。建议安装在默认路径:C:Python27添加环境变量 Path:C:Pytho
3、n27安装验证:cmd 命令中,输入 python,进入 python command line 模式安装 selenium安装 setuptools双击安装 setuptools-0.6c11.win32-py2.7.exe 即可。安装路径 C:Python27Libsite-packages必须安装 setuptools,是因为 pip 和 selenium 的安装文件 setup.py 中使用。安装 pip解压 pip-1.0.2.tar.gz,将解压文件放到 C:下,在 DOS 环境进入 C:pip-1.0.2,执行命令:python setup.py install。请注意安装路径。
4、默认在:C:Python27Scripts。安装 selenium进入 pip.exe 所在路径,运行命令行:pip install -U selenium。 (需要网络,出现下列错误.忽略)D:Python27Scriptspip install -U seleniumDownloading/unpacking selenium4Downloading selenium-2.24.0.tar.gz (1.9Mb): 1.9Mb downloadedRunning setup.py egg_info for package seleniumD:Python27libdistutilsdist.
5、py:267: UserWarning: Unknown distribution option: src_rootwarnings.warn(msg)warning: no files found matching docsapipyindex.rstInstalling collected packages: seleniumFound existing installation: selenium 2.24.0Uninstalling selenium:Successfully uninstalled seleniumRunning setup.py install for seleni
6、umD:Python27libdistutilsdist.py:267: UserWarning: Unknown distribution option: src_rootwarnings.warn(msg)warning: no files found matching docsapipyindex.rstSuccessfully installed seleniumCleaning up.安装 webdriver安装 firefox webdirver将 selenium-ide-1.6.0.xpi 拖进 firefox 浏览器,即开始安装Firefox 浏览器选装 firebug、Fi
7、rePath 插件安装 chrome webdriver解压 chromedriver.exe 到 Python 的安装目录下,如 C:Python27。添加 C:UsersAdministratorAppDataLocalGoogleChromeApplication(chrome 安装路径,这里是 win7 下的安装路径)到环境变量 path安装 ie webdriver复制 IEDriverServer.exe 到 C:Python27设置 IE 浏览器,Internet 选线安全,把各模式的“启动保护模式”设置成一样(或者全部启动,或者全部不启动) 。自定义模块添加环境变量 PYTHO
8、NPATH,定义为工程所在(自定义功能模块)的路径。5D:workspaceautoprojectsrcDemo#!/usr/bin/pythonfrom selenium import webdriverfrom mon.exceptions import TimeoutExceptionfrom selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0 import time# Create a new instance of the Chrome driver, Chrome(), Firefox
9、() or Ie() is useabledriver = webdriver.Chrome() # go to the baidu home pagedriver.get(“http:/“)# find the element thats name attribute is q (the google search box)inputElement = driver.find_element_by_xpath (“/inputname=wd“)submitElement = driver.find_element_by_xpath (“id(su)“)# type in the searchinputElement.send_keys(“Cheese!“)# submit the formsubmitElement.submit()# the page is ajaxy so the title is originally this:print driver.titledriver.quit()