python selenium模块使用出错-selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executab...

 

Python 2.7+selenium+Firefox 55.0.3

代码:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
assert "Yahoo!" in browser.title
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("seleniumhq" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:
    browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")
except NoSuchElementException:
    assert 0, "can't find seleniumhq"
browser.close()

错误信息如下:

selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

回答摘自知乎:https://www.zhihu.com/question/49568096

1. selenium 3.x开始,webdriver/firefox/webdriver.py的__init__中,executable_path="geckodriver";而2.x是executable_path="wires"
2. firefox 47以上版本,需要下载第三方driver,即geckodriver;在 的Third Party Drivers, Bindings, and Plugins下面找到 Mozilla GeckoDriver,下载到任意电脑任意目录(Mac 下放到了/usr/bin/ 和/sbin/, /bin/,/usr/local/bin/),解压后将该路径加入到PC的path(针对windows)即可。


作者:iceblue iceblue
链接:https://www.zhihu.com/question/49568096/answer/119324584
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

转载于:https://www.cnblogs.com/v-BigdoG-v/p/7851709.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
selenium.common.exceptions.WebDriverException: Message: Failed to decode response from marionette 是一个由Selenium WebDriver引发的异常,表示无法解码来自Marionette的响应。Marionette是Firefox浏览器的驱动程序,用于与Selenium WebDriver进行通信。 这个异常通常发生在以下情况下: 1. Firefox浏览器版本与Selenium WebDriver版本不兼容。 2. Firefox浏览器或Selenium WebDriver的安装文件损坏或不完整。 3. Firefox浏览器的配置文件出现问题。 解决这个问题的方法有以下几种: 1. 确保Firefox浏览器和Selenium WebDriver的版本兼容。可以在Selenium官方网站上查看版本兼容性列表,并下载相应版本的WebDriver。 2. 检查Firefox浏览器和Selenium WebDriver的安装文件是否完整且没有损坏。可以尝试重新下载并安装它们。 3. 尝试重置Firefox浏览器的配置文件。可以通过在浏览器地址栏中输入"about:support"来访问Firefox的支持页面,然后点击"重置Firefox"按钮。 下面是一个示例代码,演示了如何处理这个异常: ```python from selenium import webdriver try: # 创建Firefox浏览器实例 driver = webdriver.Firefox() # 执行其他操作 # ... except selenium.common.exceptions.WebDriverException as e: if "Failed to decode response from marionette" in str(e): print("Failed to decode response from marionette. Please check Firefox and Selenium WebDriver compatibility.") else: print("An error occurred:", str(e)) finally: # 关闭浏览器实例 driver.quit() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值