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的环境搭建的更多相关文章

  1. Redis的简单介绍及在Windows下环境搭建

    简单介绍 1,Redis是什么 最直接的还是看官方的定义吧. Redis is an open source (BSD licensed), in-memory data structure stor ...

  2. Windows GTK+ 环境搭建(详解)

    来源:http://blog.sina.com.cn/s/blog_a6fb6cc901017ygy.html Windows GTK+ 环境搭建 最近要做界面的一些东西,但是对微软提供的类库MFC不 ...

  3. Spark+ECLIPSE+JAVA+MAVEN windows开发环境搭建及入门实例【附详细代码】

    http://blog.csdn.net/xiefu5hh/article/details/51707529 Spark+ECLIPSE+JAVA+MAVEN windows开发环境搭建及入门实例[附 ...

  4. Django Python MySQL Linux 开发环境搭建

    Django Python MySQL Linux 开发环境搭建 1.安装Python 进行Python开发,首先必须安装python,对于linux 或者Mac 用户,python已经预装. 在命令 ...

  5. Python基础学习之环境搭建

    Python如今成为零基础编程爱好者的首选学习语言,这和Python语言自身的强大功能和简单易学是分不开的.今天我们将带领Python零基础的初学者完成入门的第一步——环境搭建.本文会先来区分几个在P ...

  6. Python Windows开发环境搭建

    一.下载python安装包 python官网地址:https://www.python.org/,下载windows平台包并安装 二.在Windows设置环境变量 计算机右键,选择属性,在高级系统设置 ...

  7. UI自动化测试简介及Selenium工具的介绍和环境搭建

    自动化测试简介 1.1何为自动化测试? 是把以人为驱动的测试转化为机器执行的一种过程,它是一种以程序测试程序的过程.换言之,就是以程序实现的方式来代替手工测试. 1.2自动化测试分类 分为功能自动化测 ...

  8. UI自动化测试(一)简介及Selenium工具的介绍和环境搭建

    自动化测试简介 1.1何为自动化测试? 是把以人为驱动的测试转化为机器执行的一种过程,它是一种以程序测试程序的过程.换言之,就是以程序实现的方式来代替手工测试. 1.2自动化测试分类 分为功能自动化测 ...

  9. 【Python学习】Python3 环境搭建

    参考地址:http://www.runoob.com/python3/python3-install.html Python3 环境搭建 本章节我们将向大家介绍如何在本地搭建 Python3 开发环境 ...

随机推荐

  1. 构建RESTful风格的WCF服务

    构建RESTful风格的WCF服务 RESTful Wcf是一种基于Http协议的服务架构风格. 相较 WCF.WebService 使用 SOAP.WSDL.WS-* 而言,几乎所有的语言和网络平台 ...

  2. Visual Studio 2012设置Jquery/Javascript智能提示

    Visual Studio 2012设置Jquery/Javascript智能提示 在Visual Studio 2008 Visual Studio 2010中微软已经开始支持jquery/java ...

  3. EditPlus配置C环境

    一, 目标 1, 编译 ---- 根据编写的 .c 文件, 生成 .exe 文件 2, 运行 ---- 运行 生成的 .exe文件 3, 清理 ---- 删除 生成的 .exe文件 二, 环境 1,  ...

  4. UVa-Where's Waldorf?

    题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. 《Head First Python》学习笔记03 异常处理

    异常(运行时错误): 当代码逻辑遇到意外事件时,比如打开一个文件,却发现文件不存在.这时是增加额外的代码处理逻辑,还是捕获异常呢?答案是:Python优先推荐捕获异常,然后恢复. Python异常机制 ...

  6. ON COMMIT PRESERVE ROWS

    定义声明式全局临时表的每个会话拥有自己的独特的临时表描述.当会话终止时,表行和临时表描述均会被删除. 有如下选项,可控制commit后临时表的状态: ON COMMIT DELETE ROWS:在执行 ...

  7. context.response.end()和return的区别

    最近忽然想起这个问题,上网查了很多,觉得这个网友回答的很给力,从本质上剖析了问题.最后发现这篇文章也是转载自博客园的一位网友.http://www.cnblogs.com/adolphyang/p/4 ...

  8. “System.FormatException”类型的异常在 mscorlib.dll 中发生,但未在用户代码中进行处理 其他信息: 该字符串未被识别为有效的 DateTime。

    前台用过jquery ajax将值传递到 后台的一般处理程序 并且报了这个异常 原因是:前台传递过来的时间格式不对  需要把 "/"换成 "-"   例如:20 ...

  9. TestNG中的DataProvider返回Iterator<Object[]>的妙用

    TestNG中使用DataProvider有个好处,就是: 1. 在执行用例的时候dataProvider迭代中的每组数据都是作为一个用例执行 2. 在测试数据有规律的情况下,免去了添加testXML ...

  10. hdu1044

    #include <cstdio> #include <cstring> #include <queue> using namespace std; const i ...