python-2.7 - Selenium Firefox Webdriver for python KeyError 'value'

标签 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. 据观察,当我在没有 sudo 的情况下运行程序时,观察到 IOError: [Errno 13] Permission returned: 'geckodriver.log'。这真的是必需的吗?

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

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

P.S.:深入网络学习 selenium,指出了 here 中提到的一些特定于架构的问题。和 here这些问题似乎都没有得到解决,这让我很担心。 Chrome 会是更好的选择吗?

更新:以下是 ls -alh 的输出:

-rw-r--r-- 1 root        root       238K Aug 22 17:12 geckodriver.log
-rwxrwxrwx 1 user4       user       82K  Aug 22 17:08 test.py

最佳答案

由于您在调用 webdriver.Firefox() 时尝试传递几个参数,因此我们需要考虑 executable_pathfirefox_binary 这两个参数都采用 string 类型的参数。因此,您需要将 geckodriver 二进制文件的绝对路径传递给 executable_path 以及 firefox 二进制文件到 firefox_binary。此外,您不需要使用binary=FirefoxBinary(path_bin)。以下代码块在我的 Windows 8 计算机上运行良好:

Code is based on 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 时,以下代码块必须适合您:

from selenium import webdriver

path_bin='/usr/bin/firefox'
path_dr='/usr/local/bin/geckodriver'
profile = webdriver.FirefoxProfile()
driver = webdriver.Firefox(executable_path=path_dr,firefox_profile=profile,firefox_binary=path_bin)
driver.get('https://www.google.co.in')

关于python-2.7 - Selenium Firefox Webdriver for python KeyError 'value',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45816619/

相关文章:

windows - 让 selenium、ghostdriver、phantomJS 在 Windows 7 上工作

python - 如何下载 XML 文件避免弹出窗口这种类型的文件可能会通过 ChromeDriver 和 Chrome 使用 Python 中的 Selenium 损害您的计算机

selenium - 如何编写一个 CSS Selector 选择不具有特定属性的元素?

python - 在 64 位机器的 python 2.7 中安装 NLTK

python - random.randint 不生成随机值

python-2.7 - 如何在 Centos 7 机器上为 vim 7.4 添加 python 支持?

python - 无法在python中使用selenium webdriver定位元素

javascript - 如何使用 Protractor 和 Chrome 浏览器打开新标签页

python - selenium.common.exceptions.WebDriverException : Message: unknown error: unable to discover open pages while test execution through InstaPy and selenium

python-2.7 - virtualenv 与此系统或可执行文件不兼容