前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Selenium3+python自动化50-环境搭建(firefox)

Selenium3+python自动化50-环境搭建(firefox)

作者头像
上海-悠悠
发布2018-04-08 15:58:54
1.2K0
发布2018-04-08 15:58:54
举报
文章被收录于专栏:从零开始学自动化测试

前言

有不少小伙伴在安装selenium环境后启动firefox报错,因为现在selenium升级到3.0了,跟2.0的版本还有有一点区别的。

安装环境过程中主要会遇到三个坑:

1.'geckodriver' executable needs to be in PATH

2.Expected browser binary location, but unable to find binary in default location

3.Unsupported Marionette protocol version 2, required 3

环境准备:

--python3.6

--selenium3.0

--firefox50

一、安装python

1.安装python这个简单,下载版本后傻瓜式安装就行了。

2.安装好之后,看下这个目录D:\python\Scripts,有没pip.exe和easy_install.exe(一般都有,没有的话得重新安装一次了)

3.将D:\python和D:\python\Scripts,添加到环境变量path下

二、检查pip工具

1.打开cmd,输入:pip,出现如下图界面,说明pip环境OK.

>>pip

2.要是出现异常提示:Did not provide a command,就看这篇解决:Selenium2+python自动化3-解决pip使用异常

三、安装selenium3.0

1.cmd输入:pip install selenium

代码语言:javascript
复制
>>pip install selenium

2.首次安装要看到100%完成,中途失败就重新多输入几次安装。

四、检查selenium环境

1.在cmd输入如下指令检查环境

代码语言:javascript
复制
>>python
>>from selenium import webdriver
>>driver=webdriver.Firefox()
>>driver.get("https://www.baidu.com")

2.能看到浏览器正常启动,说明环境OK,如果遇到异常就继续看下面解决方案。

五、遇到第一个坑:'geckodriver' executable needs to be in PATH

1.如果启动浏览器过程中报如下错误

代码语言:javascript
复制
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 145, in __init__
    self.service.start()
  File "D:\test\python3\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

2.这个是因为最新的selenium3.0启动firefox需要geckodriver.exe这个驱动文件。

3.下载之后,配置到环境变量path下(可以直接放python根目录)

六、遇到第二坑:Expected browser binary location, but unable to find binary in default location

1.如果启动浏览器过程中报如下错误

代码语言:javascript
复制
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 155, in __init__
    keep_alive=True)
  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session
    response = self.execute(Command.NEW_SESSION, capabilities)
  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 238, in execute
    self.error_handler.check_response(response)
  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.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

2.这个是因为firefox.exe这个文件也需要配置到环境变量path下

3.这个路径就是安装完firefox后,找到firefox.exe这个文件的地址,加到path下

七、遇到第三坑:Unsupported Marionette protocol version 2, required 3

1.如果启动浏览器过程中出现如下错误

代码语言:javascript
复制
 Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 155, in __init__
    keep_alive=True)
  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session
    response = self.execute(Command.NEW_SESSION, capabilities)
  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 238, in execute
    self.error_handler.check_response(response)
  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Unsupported Marionette protocol version 2, required 3

2.这个错误原因是firefox版本过低了,最新的selenium3.0版本支持firefox47以上的版本,升级版本就可以了

总结:整个环境的配置是python3.6+selenium3.0+firefox47以上版本,当然python用2.7版本也是可以的

要是觉得selenium3.0比较坑的话,可以继续用selenium2.0版本也是可以的,看这篇环境搭建:Selenium2+python自动化1-最新环境搭建

在安装过程中有遇到疑问和问题的,可以加selenium(python+java) QQ群交流:232607095

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-03-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 从零开始学自动化测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档