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模式

安装setuptools

方法一:

双击安装setuptools-0.6c11.win32-py2.7.exe下一步即可。

安装路径C:\Python27\Lib\site-packages

必须安装setuptools,是因为pip和selenium的安装文件setup.py中使用。

方法二:

进入Python官网https://pypi.python.org/pypi/setuptools/。看到下面的界面

 

点击ez_setup.py,在浏览器中弹出新的标签页,里面是代码部分

 

 直接打开地址为:https://bootstrap.pypa.io/ez_setup.py

全选并复制到一个新文件命名为ez_setup.py(方便起见保存到桌面)

 

 双击运行,脚本会自动识别Python版本,并下载相应的文件给你安装好,其实挺傻瓜式的。

运行过程

 

检验

打开Python目录的script文件夹查看

 

安装 pip

1.在以下地址下载最新的PIP安装文件:http://pypi.python.org/pypi/pip#downloads
2.下载pip-7.1.2.tar.gz (md5, pgp)完成之后,解压pip-7.1.2.tar.gz,将解压文件放到C:\Python27下,在DOS环境进入C:\Python27\pip-1.0.2,执行命令:python setup.py install。请注意安装路径。

3.DOS命令C:\Python27\pip-1.0.2> python setup.py install

安装 selenium

进入c:\python27\scripts

 

输入 pip install -u selenium   (这里u为大写,之前安装的时候 使用小写 导致无法安装)【这步是安装selenium 必须是在联网的情况下才可进行的】

 

验证

在验证之前需要先 from selenium import webdriver 

 

验证是否已经装好,试着启动firefox浏览器

打开pythonidle,运行如下脚本,看运行是否成功。

from selenium import webdriver

browser = webdriver.firefox() # 打开火狐浏览器

browser.get("http://www.baidu.com") # 登录百度 首页

 

安装webdriver

安装firefox webdirver

将selenium-ide-1.6.0.xpi拖进firefox浏览器,即开始安装

Firefox浏览器选装firebug、FirePath插件

安装chrome webdriver

1.解压chromedriver.exe到Python的安装目录下,如C:\Python27。

2.添加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 selenium.common.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("http://www.baidu.com")

 

# 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()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值