Python 3 Selenium KeyError:'value'问题不会初始化为Firefox的Geckodriver [英] Python 3 Selenium KeyError: 'value' Issue won't initialize Geckodriver for Firefox

查看:797
本文介绍了Python 3 Selenium KeyError:'value'问题不会初始化为Firefox的Geckodriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Python 3运行geckodriver时遇到了麻烦。我最近用一个我一直在使用的应用程序切换到Python 3,并更新了Firefox(53.0),Selenium(3.4.3)和geckodriver(0.17 0.1)。我也使用OSX并使用pip来安装所有的软件包。

  from selenium import webdriver 
from selenium .webdriver.firefox.firefox_binary导入FirefoxBinary

#设置Firefox设置
#binary = FirefoxBinary('Users / username / Applications / Firefox.app / Contents / MacOS / firefox')
#binary = FirefoxBinary('/ Applications / Firefox.app / Contents / MacOS / firefox')
#binary = FirefoxBinary('/ Applications / Firefox.app / Contents / MacOS / firefox-bin')

path ='/ usr / local / bin / geckodriver'
profile = webdriver.FirefoxProfile()

browser = webdriver.Firefox(executable_path = path,
firefox_profile = profile,
firefox_binary = binary)

browser.get(http://google.com)

网页浏览器将会启动,但是我会收到以下错误消息:

  Traceback(最近的最后一次调用):
文件/ Users / jph ubert / Desktop / AbstractionProject / py / browsertest.py,第11行,在< module>
firefox_profile = profile),
文件/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py,行155,in __init__
keep_alive = True)
File/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py ,第92行,在__init__
self.start_session(desired_capabilities,browser_profile)
文件/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/ webdriver / remote / webdriver.py,第183行,在start_session
self.capabilities = response ['value']
KeyError:'value'
pre>

我已经卸载并重新安装Firefox和geckodriver,以@Viragos的建议来确保我安装了MacOS版本。 @Debanjan让我在正确的轨道上尝试设置Firefox二进制文件,并将其包括在webdriver配置文件中,但我仍然得到相同的错误。

我已经尝试从GitHub采取Firefox二进制文件,并将其放入一个.py文件,并试图删除配置文件和可执行文件路径,但同样的问题仍然存在。我的二进制文件是在正确的位置,它的工作原理如果我自己沿着路径,点击他们的.exe文件,但我不能让我的脚本运行了。

我已经在Python 2.7上运行了Selenium,没有任何问题,从昨天开始,升级gecko和Python,我一直有问题。



谢谢!这是您的问题的答案:

使用Selenium 3.4。 x,Python 3.6.1以及geckodriver v0.16.1& Mozilla Firefox 53.0,您可以配置以下参数:


  1. firefox_binary :提供您打算使用的确切的Firefox二进制文件的绝对路径。
  2. firefox_profile :指定是否要以新的/
  3. executable_path :指定geckodriver.exe的绝对路径。

  4. ol>


    需要指出的是,当前的Selenium-Python绑定对于geckodriver来说是不稳定的,并且看起来像架构特定的。你可以找到github 讨论 merge 这里。所以当初始化 webdriver

    时,您可能还需要传递firefox二进制文件的绝对路径 firefox_binary / blockquote>

    以下代码块将按照我们通过前面提到的参数设置的配置打开Firefox浏览器:

      from selenium import webdriver 
    from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

    binary = FirefoxBinary('C:\\ Program Files \ \ Mozilla Firefox \\ firefox.exe')
    profile = webdriver.FirefoxProfile()
    path =C:\\\\ Utility \\BrowserDrivers\\geckodriver.exe

    browser = webdriver.Firefox(executable_path = path,firefox_profile = profile,firefox_binary = binary)
    browser.get(http://google.com)

    让我知道这个答案是否是您的问题。


    I'm having trouble running geckodriver with Python 3. I recently switched to Python 3 with an application that I've been working on, and have updated Firefox(53.0), Selenium(3.4.3), and geckodriver(0.17.1). I'm also using OSX and used pip to install all of my packages.

    from selenium import webdriver
    from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
    
    # Set Firefox Settings
    # binary = FirefoxBinary('Users/username/Applications/Firefox.app/Contents/MacOS/firefox')
    # binary = FirefoxBinary('/Applications/Firefox.app/Contents/MacOS/firefox')
    # binary = FirefoxBinary('/Applications/Firefox.app/Contents/MacOS/firefox-bin')
    
    path = '/usr/local/bin/geckodriver'
    profile = webdriver.FirefoxProfile()
    
    browser = webdriver.Firefox(executable_path=path,
                            firefox_profile=profile,
                            firefox_binary=binary)
    
    browser.get("http://google.com")
    

    The web browser will start, but then I will receive the following error:

    Traceback (most recent call last):
    File "/Users/jphubert/Desktop/AbstractionProject/py/browsertest.py", line 11, in <module>
    firefox_profile=profile),
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 155, in __init__
    keep_alive=True)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 92, in __init__
    self.start_session(desired_capabilities, browser_profile)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 183, in start_session
    self.capabilities = response['value']
    KeyError: 'value'
    

    I have uninstalled and reinstalled Firefox and geckodriver, taking @Viragos advice to ensure I installed the MacOS version. @Debanjan set me on the right track to try and set the Firefox binary and include it in the webdriver profile but I'm still getting the same error.

    I've tried taking the Firefox binary from GitHub and putting it in a .py file as well, and have made attempts to remove the profile and executable path, but the same problem persists. My binary files are in the correct location and it works if I follow the path myself and click through their .exe files, but I can't make my script run anymore.

    I've been running Selenium without any issues on Python 2.7, and only since yesterday, upgrading gecko and Python, have I been having problems.

    Thank you!

    解决方案

    Here is the Answer to your Question:

    Using Selenium 3.4.x, Python 3.6.1 along with geckodriver v0.16.1 & Mozilla Firefox 53.0, you can configure the following parameters:

    1. firefox_binary: To provide the absolute path of the exact Firefox Binary you intend to use.
    2. firefox_profile: To specify if you want to start with a new/existing Firefox Profile.
    3. executable_path: To specify absolute path of the geckodriver.exe.

    It is to be noted that the current Selenium-Python binding is unstable with geckodriver and looks to be Architecture specific. You can find the github discussion and merge here. So you may additionally need to pass the absolute path of the firefox binary as firefox_binary argument while initializing the webdriver

    The following code block will open a Firefox Browser as per the configuration we set through the previous mentioned parameters:

    from selenium import webdriver
    from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
    
    binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
    profile = webdriver.FirefoxProfile()
    path = "C:\\Utility\\BrowserDrivers\\geckodriver.exe"
    
    browser = webdriver.Firefox(executable_path=path, firefox_profile=profile,firefox_binary=binary)
    browser.get("http://google.com")
    

    Let me know if this Answers your Question.

    这篇关于Python 3 Selenium KeyError:'value'问题不会初始化为Firefox的Geckodriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆