selenium+python在Windows的环境搭建
1 python下载安装
python早已安装,不再多说。因为开发使用的python2.7,所以同样使用2.7
2 打开Powershell, 输入python -m pip install selenium 。
提示:
Requirement already satisfied (use --upgrade to upgrade): selenium in e:\python27\lib\site-packages
You are using pip version 7.1.2, however version 9.0.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
3 再输入 python -m pip install --upgrade pip 。
4 使用python IDLE, 输入例子代码
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time browser = webdriver.Firefox()
browser.get('http://www.yahoo.com')
assert 'Yahoo' in browser.title
elem = browser.find_element_by_name('p')
elem.send_keys('seleniumhq'+ Keys.RETURN)
time.sleep(0.2)
try:
browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")
except NoSuchElementException:
assert 0, "can't find seleniumhq"
browser.quit()
报错:
Traceback (most recent call last):
File "C:/Users/william/Desktop/selenium/seleniumhq.py", line 6, in <module>
browser = webdriver.Firefox()
File "E:\Python27\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 140, in __init__
self.service.start()
File "E:\Python27\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
os.path.basename(self.path), self.start_error_message)
WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
5 打开google, 搜索 WebDriverException: Message: 'geckodriver' executable needs to be in PATH。 或者百度,搜索 : selenium geckodriver。
6 原因是, firefox 47以上版本,需要下载第三方driver,即geckodriver
7 打开 http://docs.seleniumhq.org/download/ 。 下载 Mozilla GeckoDriver ,解压。这里,我将exe文件放到python的Scripts中,免了新加path。
8 执行python,发现仍报错。
Traceback (most recent call last):
File "C:/Users/william/Desktop/selenium/seleniumhq.py", line 6, in <module>
browser = webdriver.Firefox()
File "E:\Python27\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 150, in __init__
keep_alive=True)
File "E:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__
self.start_session(desired_capabilities, browser_profile)
File "E:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session
response = self.execute(Command.NEW_SESSION, capabilities)
File "E:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "E:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line
看意思,应该是firefox的安装路径没有找到的原因。google一下。
9 python代码中添加
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe')
browser = webdriver.Firefox(firefox_binary=binary)
再执行,没有报错了。执行完后,python shell会显示断言的提示:can't find seleniumhq, 并且浏览器没有退出。这是为什么呢? 结果应该是find seleniumhq啊
难道是 assert 0, "can't find seleniumhq" 后面的0的缘故?
10 百度搜索 assert 0。 后面加0,代表永远为假,即永远会显示 can't find seleniumhq。但这个的发生是在exception之后啊。
11 去掉0试一试。不再显示断言提示
12 如果将xpath中的条件改一改呢? 改成 http://seleniumhqwww.org 。 仍然没有显示断言,并浏览器退出。这里就不对了。因为这个xpath应该是匹配不到的。
13 再查断言assert的用法
assert的用法,常见的文章中提到的都是,assert用来声明某个条件是真的。如果非真,就引发一个错误,抛出这个错误AssertError并包含错误信息。
例如 assert x>0,"x is not zero or negative"
这样的话,从语法上讲,assert 0, "can't find seleniumhq" 没有问题。那么就是真没有find element
14 是不是sleep(0.2)时间太短,element还没有出现造成的呢? 改0.2为5,再分别试,OK。解决。
selenium+python在Windows的环境搭建的更多相关文章
- Redis的简单介绍及在Windows下环境搭建
简单介绍 1,Redis是什么 最直接的还是看官方的定义吧. Redis is an open source (BSD licensed), in-memory data structure stor ...
- Windows GTK+ 环境搭建(详解)
来源:http://blog.sina.com.cn/s/blog_a6fb6cc901017ygy.html Windows GTK+ 环境搭建 最近要做界面的一些东西,但是对微软提供的类库MFC不 ...
- Spark+ECLIPSE+JAVA+MAVEN windows开发环境搭建及入门实例【附详细代码】
http://blog.csdn.net/xiefu5hh/article/details/51707529 Spark+ECLIPSE+JAVA+MAVEN windows开发环境搭建及入门实例[附 ...
- Django Python MySQL Linux 开发环境搭建
Django Python MySQL Linux 开发环境搭建 1.安装Python 进行Python开发,首先必须安装python,对于linux 或者Mac 用户,python已经预装. 在命令 ...
- Python基础学习之环境搭建
Python如今成为零基础编程爱好者的首选学习语言,这和Python语言自身的强大功能和简单易学是分不开的.今天我们将带领Python零基础的初学者完成入门的第一步——环境搭建.本文会先来区分几个在P ...
- Python Windows开发环境搭建
一.下载python安装包 python官网地址:https://www.python.org/,下载windows平台包并安装 二.在Windows设置环境变量 计算机右键,选择属性,在高级系统设置 ...
- UI自动化测试简介及Selenium工具的介绍和环境搭建
自动化测试简介 1.1何为自动化测试? 是把以人为驱动的测试转化为机器执行的一种过程,它是一种以程序测试程序的过程.换言之,就是以程序实现的方式来代替手工测试. 1.2自动化测试分类 分为功能自动化测 ...
- UI自动化测试(一)简介及Selenium工具的介绍和环境搭建
自动化测试简介 1.1何为自动化测试? 是把以人为驱动的测试转化为机器执行的一种过程,它是一种以程序测试程序的过程.换言之,就是以程序实现的方式来代替手工测试. 1.2自动化测试分类 分为功能自动化测 ...
- 【Python学习】Python3 环境搭建
参考地址:http://www.runoob.com/python3/python3-install.html Python3 环境搭建 本章节我们将向大家介绍如何在本地搭建 Python3 开发环境 ...
随机推荐
- win8商店应用验证,二进制文件是在调试模式下生成的解决方案。
程序是在release模式下生成的,并且arm和x64通过了验证,但是x86就出现了这个奇葩问题. 搞了半天发现是要把“优化代码”的选项勾上.
- C++中内存泄漏的检测方法介绍
C++中内存泄漏的检测方法介绍 首先我们需要知道程序有没有内存泄露,然后定位到底是哪行代码出现内存泄露了,这样才能将其修复. 最简单的方法当然是借助于专业的检测工具,比较有名如BoundsCheck, ...
- MTD设备驱动
MTD(memory technology device):内存技术设备 是linux用于描述ROM,NAND,NOR等内存设备的子系统的抽象 MTD设备可以按块读写也可以按字节读写,也就是说MTD设 ...
- iOS开发-OC语言 (五)字典
字典 主要知识点: 1.NSDictionary 类 2.NSMutableDictionary 类 3.了解NSMutableDictionary 与 NSDictionary 的继承关系 4.补充 ...
- selenium自动化过程中遇到的小问题(未完待续)
1.chrome浏览器调用不起来 代码没出错的情况下,检查下chrome浏览器的版本与chromedriver.exe的版本是否匹配;下面的表格是根据网上及官网整理的chromedriver与chro ...
- ActiveMQ的简单例子应用
ActiveMQ是一种消息中间件,它实现了JMS规范,提供点对点和订阅-发布两种模式.下面介绍下ActiveMQ的使用: 一.环境的搭建 首先我们需要下载ActiveMQ的安装包,下载地址http:/ ...
- Idea高级用法
一.快速打开Action: Ctrl+Shift+A 列表中会列出所有的action,对应于idea的各种操作,例如: 输入backgroud,可以为编辑器设置背景 输入restclient,可以打开 ...
- [大山中学dp常练-4 Rounds]
Round#1 2016.9.28 这次晚练十分sb 有点浪费时间 全是dp题 先说过程 3分钟草了第一题之后感觉好像有点水 然后翻了翻题目 看了看第一第四题两颗星 其他三颗星 然后一下子第二题题目太 ...
- Nuget 学习一
初识Nuget 对应C#开发的人员来说, DLL和相关的引用 并不陌生吧,通过手动拷贝DLL 存在很多问题, 比如SOA的实体DLL,开发者和使用者的DLL不一致,就可能存在字段的不一致, 通过Nug ...
- ffmpeg的安装--opencv视频处理必备
安装yasm(ffmpeg必备)wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gztar xzvf yasm-1. ...