博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
selenium + python自动化测试环境搭建
阅读量:4289 次
发布时间:2019-05-27

本文共 7894 字,大约阅读时间需要 26 分钟。

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包:

解压,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 webdriverfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.common.keys import Keysimport timebrowser = webdriver.Firefox() # Get local session of firefoxbrowser.get("http://www.yahoo.com") # Load pageassert "Yahoo!" in browser.titleelem = browser.find_element_by_name("p") # Find the query boxelem.send_keys("seleniumhq" + Keys.RETURN)time.sleep(0.2) # Let the page load, will be added to the APItry:    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的一份不错文档

from selenium import webdriverfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.common.keys import Keysimport timebrowser = webdriver.Firefox() # Get local session of firefoxbrowser.get("http://www.yahoo.com") # Load pageassert "Yahoo!" in browser.titleelem = browser.find_element_by_name("p") # Find the query boxelem.send_keys("seleniumhq" + Keys.RETURN)time.sleep(0.2) # Let the page load, will be added to the APItry:    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的一份不错文档

from selenium import webdriverfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.common.keys import Keysimport timebrowser = webdriver.Firefox() # Get local session of firefoxbrowser.get("http://www.yahoo.com") # Load pageassert "Yahoo!" in browser.titleelem = browser.find_element_by_name("p") # Find the query boxelem.send_keys("seleniumhq" + Keys.RETURN)time.sleep(0.2) # Let the page load, will be added to the APItry:    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 webdriverfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.common.keys import Keysimport timebrowser = webdriver.Firefox() # Get local session of firefoxbrowser.get("http://www.yahoo.com") # Load pageassert "Yahoo!" in browser.titleelem = browser.find_element_by_name("p") # Find the query boxelem.send_keys("seleniumhq" + Keys.RETURN)time.sleep(0.2) # Let the page load, will be added to the APItry:    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的一份不错文档

from selenium import webdriverfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.common.keys import Keysimport timebrowser = webdriver.Firefox() # Get local session of firefoxbrowser.get("http://www.yahoo.com") # Load pageassert "Yahoo!" in browser.titleelem = browser.find_element_by_name("p") # Find the query boxelem.send_keys("seleniumhq" + Keys.RETURN)time.sleep(0.2) # Let the page load, will be added to the APItry:    browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")except NoSuchElementException:    assert 0, "can't find seleniumhq"browser.close()
# coding = utf-8from selenium import webdriverdriver = webdriver.Chrome()driver.get('http://radar.kuaibo.com')print driver.titledriver.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 webdriverfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.common.keys import Keysimport timebrowser = webdriver.Firefox() # Get local session of firefoxbrowser.get("http://www.yahoo.com") # Load pageassert "Yahoo!" in browser.titleelem = browser.find_element_by_name("p") # Find the query boxelem.send_keys("seleniumhq" + Keys.RETURN)time.sleep(0.2) # Let the page load, will be added to the APItry:    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的一份不错文档

from selenium import webdriverfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.common.keys import Keysimport timebrowser = webdriver.Firefox() # Get local session of firefoxbrowser.get("http://www.yahoo.com") # Load pageassert "Yahoo!" in browser.titleelem = browser.find_element_by_name("p") # Find the query boxelem.send_keys("seleniumhq" + Keys.RETURN)time.sleep(0.2) # Let the page load, will be added to the APItry:    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://www.cnblogs.com/fnng/archive/2013/05/29/3106515.html
你可能感兴趣的文章
elasticsearch7.3版本环境搭建(三)可视化管理后台kibana的汉化(设置中文界面)
查看>>
记录一次DDos攻击实战
查看>>
分享一首小诗--《致程序员》
查看>>
为什么百度只抓取了首页而不抓取我的网站的内页的原因分析
查看>>
年薪170万的阿里P8级员工征婚有感--话说阿里真有钱,这员工要求的条件真多
查看>>
又是一年桂花飘香时,祝我们伟大的祖国70年华诞更加繁荣昌盛,祝大家国庆节快乐
查看>>
谷歌浏览器chrome即将在2020年底停止支持flash,我们程序员该怎么办
查看>>
如何将数据采集到 Elasticsearch 服务
查看>>
面试官:mysql表设计要注意什么?
查看>>
一些精致小众网站收集录
查看>>
计算机科学探秘之linux发展史
查看>>
程序员每天早上早来10分钟的好处
查看>>
互联网30年,泡沫如梦,一个个泡沫和风口过后,会是什么样的结局
查看>>
升级centos 6.8 服务器的gcc
查看>>
网络案例分析之999皮炎平出鹤顶红色号的口红
查看>>
API网关在微服务架构中的应用,这一篇就够了
查看>>
微服务部署:蓝绿部署、滚动部署、灰度发布、金丝雀发布
查看>>
架构成长之路:Spring Cloud微服务如何实现熔断降级?
查看>>
JVM发生内存溢出的8种原因、及解决办法
查看>>
SpringBoot2.0 基础案例(12):基于转账案例,演示事务管理操作
查看>>