Selenium Firefox webdriver for python KeyError'value'

时间:2017-08-22 11:39:41

标签: python-2.7 selenium selenium-webdriver ubuntu-16.04 selenium-firefoxdriver

我开始使用以下基本python脚本来使用selenium

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select, WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

path_bin='/usr/bin/firefox'
path_dr='/usr/local/bin/geckodriver'
profile = webdriver.FirefoxProfile()
binary=FirefoxBinary(path_bin)
self.driver = webdriver.Firefox(executable_path=path_dr,firefox_profile=profile,firefox_binary=binary)
self.driver.implicitly_wait(30)

然后,我尝试执行两次(第一次没有sudo,第二次执行sudo),如下所示:

user4@pc-4:~/Scripts$ python test.py 
Traceback (most recent call last):
  File "test.py", line 2049, in <module>
    self.driver = webdriver.Firefox(executable_path=path_dr,firefox_profile=profile,firefox_binary=binary)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 144, in __init__
    self.service = Service(executable_path, log_path=log_path)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/service.py", line 44, in __init__
    log_file = open(log_path, "a+") if log_path is not None and log_path != "" else None
IOError: [Errno 13] Permission denied: 'geckodriver.log'
user4@pc-4:~/Scripts$ sudo python test.py 
Traceback (most recent call last):
  File "test.py", line 2049, in <module>
    self.driver = webdriver.Firefox(executable_path=path_dr,firefox_profile=profile,firefox_binary=binary)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 155, in __init__
    keep_alive=True)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 92, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 183, in start_session
    self.capabilities = response['value']
KeyError: 'value'

所以,我必须了解以下内容:

  1. 我在没有IOError: [Errno 13] Permission denied: 'geckodriver.log'的情况下运行程序时观察到sudo。这真的需要吗?

  2. 即使打开了浏览器窗口,尽管已经解决了Firefox的所有依赖关系(我使用的是Ubuntu 16.04),例如geckodriver,但我收到了上述错误{{1} }。我该如何解决这个问题?我是否必须对代码进行任何更改以避免这种情况?

  3. 我可以使用一些有用的建议/提示/调整来让我的程序运行。

    P.S。:进入网络学习硒,指出herehere中提到的一些特定于体系结构的问题,但似乎没有一个问题得到解决,而且这些都与我有关。铬是一个更好的选择吗?

    更新:以下是KeyError: 'value'的输出:

    ls -alh

2 个答案:

答案 0 :(得分:1)

当您尝试在调用 webdriver.Firefox() 时传递一些参数时,我们需要考虑 executable_path { {1}} 这两个参数都采用 firefox_binary 类型的参数。因此,您需要将 string 二进制的绝对路径传递给 geckodriver ,并将 executable_path的绝对路径二进制到 firefox 。此外,您不需要使用firefox_binary。以下代码块在我的Windows 8计算机上正常工作:

  

代码基于 binary=FirefoxBinary(path_bin)

Python 3.6.1

当您在from selenium import webdriver path_bin=r'C:\Program Files\Mozilla Firefox\firefox.exe' path_dr=r'C:\Utility\BrowserDrivers\geckodriver.exe' profile = webdriver.FirefoxProfile() driver = webdriver.Firefox(executable_path=path_dr,firefox_profile=profile,firefox_binary=path_bin) driver.get('https://www.google.co.in') 时,以下代码块必须适合您:

ubuntu

答案 1 :(得分:0)

我在

上也遇到了同样的问题
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 183, in start_session
self.capabilities = response['value'] 
KeyError: 'value'

因此,我已经将硒和geckodriver更新为最新版本。这就是我的解决方案。