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 开发环境 ...
随机推荐
- 写20万数据到Excel只需9秒
on my god,写20万数据到Excel只需9秒 还是菜鸟时,在某个.Net项目中,用户需要从业务系统导出Report,而数据量通常都在上万条以上,最初采用的方式就是在服务器端用NPOI生成E ...
- HDFS中文件的压缩与解压
HDFS中文件的压缩与解压 文件的压缩有两大好处:1.可以减少存储文件所需要的磁盘空间:2.可以加速数据在网络和磁盘上的传输.尤其是在处理大数据时,这两大好处是相当重要的. 下面是一个使用gzip工具 ...
- 64位CentOS 6.0下搭建LAMP环境
系统环境:Centos6.0 x64 1.确认搭建LAMP所需要的环境是否已经安装 [root@centos6 ~]# rpm -q make gcc gcc-c++ zlib-devel libai ...
- [珠玑之椟]估算的应用与Little定律
[珠玑之椟]估算的应用与Little定律 估算的数据主要依赖于所能获得的数据和常识,有时还包括实践而不仅仅是理论.它常常作为一个大问题中的子问题,恰当地估算可以省去精确计算的时间和开销.在计算机领域, ...
- sharepoint 2013 自定义列表eventhandle权限控制
记录一下如何在sharepoint server 2013自定义列表中,使用eventhandle控制自定义列表custom list的条目item权限. ///<summary> /// ...
- Apache Tomcat部署java web项目
本教程安装环境为windows服务器 在服务器中下载安装JDK JDK下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8 ...
- servlet笔记,配置与 http相关
tomcat配置环境变量: JAVA_HOME= 指向你的jdk的主目录(bin目录的上一层) server.xml: <Context path="/myweb2&qu ...
- 【锋利的Jquery】读书笔记七
第七章 jquery插件 管理cookie的插件--cookie jquery插件太多没什么好讲的,百度太多 说以下 cookie插件 <!DOCTYPE html> <html& ...
- 带分页、过滤条件的DSL
"query": { "bool": { "must_not": { "match_phrase": { "p ...
- Java learning notes (1):Basic Knowlege points
Basic Knowlege points: 1: it's necessary that there is only one public class in per .java file 2: .j ...