资源描述
Selenium+Python配置
目录
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
自定义模块 4
Demo 5
环境部署
安装程序
python-2.7.2.msi,python安装程序
setuptools-0.6c11.win32-py2.7.exe,安装selenium必备软件
pip-1.0.2.tar.gz
selenium-2.18.1.tar.gz(pip命令下载安装),selenium安装程序
selenium-ide-1.6.0.xpi,firefoxWebDriver
selenium-server-standalone-2.18.0.jar,ieWebDriver
chromedriver.exe(chromedriver_win_18.0.1022.0.zip ),chromeWebDriver
ufida.zip
安装python
双击安装python-2.7.2.msi即可。建议安装在默认路径:C:\Python27
添加环境变量Path:C:\Python27
安装验证:cmd命令中,输入python,进入python command line模式
安装selenium
安装setuptools
双击安装setuptools-0.6c11.win32-py2.7.exe即可。
安装路径C:\Python27\Lib\site-packages
必须安装setuptools,是因为pip和selenium的安装文件setup.py中使用。
安装 pip
解压pip-1.0.2.tar.gz,将解压文件放到C:\下,在DOS环境进入C:\pip-1.0.2,执行命令:python setup.py install。请注意安装路径。
默认在:C:\Python27\Scripts。
安装 selenium
进入pip.exe所在路径,运行命令行:pip install -U selenium。(需要网络,出现下列错误.忽略)
D:\Python27\Scripts>pip install -U selenium
Downloading/unpacking selenium
Downloading selenium-2.24.0.tar.gz (1.9Mb): 1.9Mb downloaded
Running setup.py egg_info for package selenium
D:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution option: 'src_root'
warnings.warn(msg)
warning: no files found matching 'docs\api\py\index.rst'
Installing collected packages: selenium
Found existing installation: selenium 2.24.0
Uninstalling selenium:
Successfully uninstalled selenium
Running setup.py install for selenium
D:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution option: 'src_root'
warnings.warn(msg)
warning: no files found matching 'docs\api\py\index.rst'
Successfully installed selenium
Cleaning up...
安装webdriver
安装firefox webdirver
将selenium-ide-1.6.0.xpi拖进firefox浏览器,即开始安装
Firefox浏览器选装firebug、FirePath插件
安装chrome webdriver
解压chromedriver.exe到Python的安装目录下,如C:\Python27。
添加C:\Users\Administrator\AppData\Local\Google\Chrome\Application\(chrome安装路径,这里是win7下的安装路径)到环境变量path
安装 ie webdriver
复制IEDriverServer.exe到C:\Python27
设置IE浏览器,Internet选线安全,把各模式的“启动保护模式”设置成一样(或者全部启动,或者全部不启动)。
自定义模块
添加环境变量PYTHONPATH,定义为工程所在(自定义功能模块)的路径。
D:\workspace\autoproject\src
Demo
#!/usr/bin/python
from selenium import webdriver
from mon.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
import time
# Create a new instance of the Chrome driver, Chrome(), Firefox() or Ie() is useable
driver = webdriver.Chrome()
# go to the baidu home page
driver.get("")
# find the element that's name attribute is q (the google search box)
inputElement = driver.find_element_by_xpath ("//input[@name='wd']")
submitElement = driver.find_element_by_xpath ("id('su')")
# type in the search
inputElement.send_keys("Cheese!")
# submit the form
submitElement.submit()
# the page is ajaxy so the title is originally this:
print driver.title
driver.quit()
5
展开阅读全文