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。解决。

posted @ 2016-12-27 11:38  guohuino2  阅读(455)  评论(0编辑  收藏  举报