selenium + python自动化测试环境搭建

本文详细介绍了如何在Windows和Ubuntu环境下安装Python及Selenium,并提供了使用Selenium进行网页自动化操作的示例代码。

window安装步骤:

 

  1、下载python安装。

  https://www.python.org/downloads/release/python-351/


根据自己的操作系统32/64 位,选择相应的版本。

安装过程我就没必要描述,我的安装目录为:C:\Python35  在C盘创建一个文件夹Python35,然后自定义安装


 2、进入cmd(windows命令提示符)下面输入"python"命令。一定要到安装Pythond的目录下输入Pyhon,比如cd到Python35

(如果提示python不是内部或外部命令!别急,去配置一下环境变量吧)

修改我的电脑->属性->高级->环境变量->系统变量中的PATH为:

变量名:PATH

变量值:;C:\Python35;C:\Python35\Scripts; 

 

3、安装selenium

3.1、通过pip 安装

在C盘创建一个文件夹 selenium

命令行输入:C:\>python -m pip install selenium

3.2、通过下载包安装

或者直接下载selenium包:

https://pypi.python.org/pypi/selenium

解压,cmd进入目录:

C:\selenium\selenium2.53.5> python3 setup.py install

 

ubuntu 下安装方式:

 

1、安装:setuptools

root@fnngj-H24X:~# apt-get install python-setuptools

2、安装pip

root@fnngj-H24X:/home/fnngj/python# tar -zxvf pip-1.4.1.tar.gz

root@fnngj-H24X:/home/fnngj/python# cd pip-1.4.1/ 

root@fnngj-H24X:/home/fnngj/python# python setup.py install

3、安装selenium

root@fnngj-H24X:/home/fnngj/python/pip-1.4.1# pip install -U selenium

 

恭喜~! 你前期工作已经做了,上面的步骤确实有些繁琐,但是并不难,不过我们已经完成成了,下面体验一下成果吧! 拿python网站上的例子:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
assert "Yahoo!" in browser.title
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("seleniumhq" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:
    browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")
except NoSuchElementException:
    assert 0, "can't find seleniumhq"
browser.close()
复制代码

 


 

(运行过程中如果出现错误: 

WebDriverException: Message: u'Unexpected error launching Internet Explorer.

 Protected Mode settings are not the same for all zones. Enable Protected Mo

de must be set to the same value (enabled or disabled) for all zones.' 

更改IE的internet选项->安全,将Internet/本地Internet/受信任的站定/受限制的站点中的启用保护模式全部去 掉勾,或者全部勾上。)

 

-----------------------------------------

selenium + python的一份不错文档

http://selenium.googlecode.com/git/docs/api/py/index.html

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
assert "Yahoo!" in browser.title
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("seleniumhq" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:
    browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")
except NoSuchElementException:
    assert 0, "can't find seleniumhq"
browser.close()
复制代码

 


 

(运行过程中如果出现错误1: 

WebDriverException: Message: u'Unexpected error launching Internet Explorer.

 Protected Mode settings are not the same for all zones. Enable Protected Mo

de must be set to the same value (enabled or disabled) for all zones.' 

更改IE的internet选项->安全,将Internet/本地Internet/受信任的站定/受限制的站点中的启用保护模式全部去 掉勾,或者全部勾上。)

 运行过程中如果出现错误2: selenium.common.exceptions.WebDriverException: Message: Unsupported Marionette protocol version 2, required 3  解决办法:将火狐浏览器升级到最新的

-----------------------------------------

selenium + python的一份不错文档

http://selenium.googlecode.com/git/docs/api/py/index.html

复制代码
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
assert "Yahoo!" in browser.title
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("seleniumhq" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:
    browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")
except NoSuchElementException:
    assert 0, "can't find seleniumhq"
browser.close()
复制代码

===========================如果想通过其它浏览器(IE Chrome)运行脚本=================================

 

安装Chrome driver

chrome driver的下载地址在这里

  1. 下载解压,你会得到一个chromedriver.exe文件(我点开,运行提示started no prot 9515 ,这是干嘛的?端口9515被占了?中间折腾了半天),后来才知道需要把这家伙放到chrome的安装目录下...\Google\Chrome\Application\ ,然后设置path环境变量,把chrome的安装目录(我的:C:\Program Files\Google\Chrome\Application),然后再调用运行:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
assert "Yahoo!" in browser.title
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("seleniumhq" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:
    browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")
except NoSuchElementException:
    assert 0, "can't find seleniumhq"
browser.close()
复制代码

 


 

(运行过程中如果出现错误: 

WebDriverException: Message: u'Unexpected error launching Internet Explorer.

 Protected Mode settings are not the same for all zones. Enable Protected Mo

de must be set to the same value (enabled or disabled) for all zones.' 

更改IE的internet选项->安全,将Internet/本地Internet/受信任的站定/受限制的站点中的启用保护模式全部去 掉勾,或者全部勾上。)

 

-----------------------------------------

selenium + python的一份不错文档

http://selenium.googlecode.com/git/docs/api/py/index.html

复制代码
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
assert "Yahoo!" in browser.title
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("seleniumhq" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:
    browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")
except NoSuchElementException:
    assert 0, "can't find seleniumhq"
browser.close()
复制代码

复制代码
# coding = utf-8

from selenium import webdriver

driver = webdriver.Chrome()

driver.get('http://radar.kuaibo.com')

print driver.title

driver.quit()
复制代码

又报了个错:

Chrome version must be >= 27.0.1453.0\n  (Driver info: chromedriver=2.0,platform=Windows NT 5.1 SP3 x86)

说我chrome的版本没有大于27.0.1453.0 ,这个好办,更新到最新版本即可。

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
assert "Yahoo!" in browser.title
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("seleniumhq" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:
    browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")
except NoSuchElementException:
    assert 0, "can't find seleniumhq"
browser.close()
复制代码

 


 

(运行过程中如果出现错误: 

WebDriverException: Message: u'Unexpected error launching Internet Explorer.

 Protected Mode settings are not the same for all zones. Enable Protected Mo

de must be set to the same value (enabled or disabled) for all zones.' 

更改IE的internet选项->安全,将Internet/本地Internet/受信任的站定/受限制的站点中的启用保护模式全部去 掉勾,或者全部勾上。)

 

-----------------------------------------

selenium + python的一份不错文档

http://selenium.googlecode.com/git/docs/api/py/index.html

复制代码
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
assert "Yahoo!" in browser.title
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("seleniumhq" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:
    browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")
except NoSuchElementException:
    assert 0, "can't find seleniumhq"
browser.close()
复制代码

安装IE driver

在新版本的webdriver中,只有安装了ie driver使用ie进行测试工作。

ie driver的下载地址在这里,记得根据自己机器的操作系统版本来下载相应的driver。

暂时还没尝试,应该和chrome的安装方式类似。

 

记得配置IE的保护模式

如果要使用webdriver启动IE的话,那么就需要配置IE的保护模式了。

IE里的保护模式都选上或都勾掉就可以了。

 

 

 乙醇的安装方式:

 http://easonhan007.github.io/python/2013/05/07/setup-env/

转载从:虫师
http://www.cnblogs.com/fnng/archive/2013/05/29/3106515.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值