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

简介: 前言 有不少小伙伴在安装selenium环境后启动firefox报错,因为现在selenium升级到3.0了,跟2.0的版本还有有一点区别的。 安装环境过程中主要会遇到三个坑: 1.'geckodriver' executable needs to be in PATH 2.

 前言

有不少小伙伴在安装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

>>pip install selenium

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

 

四、检查selenium环境

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

>>python

>>from selenium import webdriver

>>driver=webdriver.Firefox()

>>driver.get("https://www.baidu.com")

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

 

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

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

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.如果启动浏览器过程中报如下错误

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.如果启动浏览器过程中出现如下错误

 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群交流:646645429

要是觉得对你有帮助,就在右下角点个赞吧!

selenium+python高级教程》已出书:selenium webdriver基于Python源码案例

(购买此书送对应PDF版本)

 

相关文章
|
3月前
|
安全 JavaScript 开发者
Python 自动化办公神器|一键转换所有文档为 PDF
本文介绍一个自动化批量将 Word、Excel、PPT、TXT、HTML 及图片转换为 PDF 的 Python 脚本。支持多格式识别、错误处理与日志记录,适用于文档归档、报告整理等场景,大幅提升办公效率。仅限 Windows 平台,需安装 Office 及相关依赖。
183 0
|
4月前
|
Web App开发 存储 前端开发
Python+Selenium自动化爬取携程动态加载游记
Python+Selenium自动化爬取携程动态加载游记
|
28天前
|
存储 数据采集 监控
Python定时爬取新闻网站头条:从零到一的自动化实践
在信息爆炸时代,本文教你用Python定时爬取腾讯新闻头条,实现自动化监控。涵盖请求、解析、存储、去重、代理及异常通知,助你构建高效新闻采集系统,适用于金融、电商、媒体等场景。(238字)
241 2
|
2月前
|
SQL 测试技术 数据库
healenium+python+selenium
上次介绍了如何利用healenium+java+selenium来实现selenium的自愈,这次介绍如何healenium+python+selenium。关于healenium+python+selenium网上资料更少,并且甚至是错误的。在著名的书籍《软件测试权威指南中》也是有一定问题的。现在介绍如下
144 4
|
2月前
|
监控 测试技术 API
n8n自动化测试教程 (1):环境搭建与初识n8n
n8n是一款开源、可视化的工作流自动化工具,测试工程师可通过拖拽节点快速构建API测试流程,实现测试编排、数据管理、自动化监控与告警等功能,提升测试效率与覆盖率。
|
2月前
|
数据采集 监控 Shell
无需Python:Shell脚本如何成为你的自动化爬虫引擎?
Shell脚本利用curl/wget发起请求,结合文本处理工具构建轻量级爬虫,支持并行加速、定时任务、增量抓取及分布式部署。通过随机UA、异常重试等优化提升稳定性,适用于日志监控、价格追踪等场景。相比Python,具备启动快、资源占用低的优势,适合嵌入式或老旧服务器环境,复杂任务可结合Python实现混合编程。
|
4月前
|
存储 数据采集 数据可视化
Python自动化分析知网文献:爬取、存储与可视化
Python自动化分析知网文献:爬取、存储与可视化
|
4月前
|
数据采集 存储 监控
Python爬虫自动化:定时监控快手热门话题
Python爬虫自动化:定时监控快手热门话题
|
测试技术 索引 Python
Python接口自动化测试框架(练习篇)-- 数据类型及控制流程(一)
本文提供了Python接口自动化测试中的编程练习,包括计算器、猜数字、猜拳和九九乘法表等经典问题,涵盖了数据类型、运算、循环、条件控制等基础知识的综合应用。
164 1
|
8月前
|
机器学习/深度学习 设计模式 测试技术
Python 高级编程与实战:构建自动化测试框架
本文深入探讨了Python中的自动化测试框架,包括unittest、pytest和nose2,并通过实战项目帮助读者掌握这些技术。文中详细介绍了各框架的基本用法和示例代码,助力开发者快速验证代码正确性,减少手动测试工作量。学习资源推荐包括Python官方文档及Real Python等网站。

热门文章

最新文章

推荐镜像

更多