用于 python KeyError 'value' 的 Selenium Firefox webdriver [英] Selenium Firefox webdriver for python KeyError 'value'

查看:31
本文介绍了用于 python KeyError 'value' 的 Selenium Firefox webdriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从使用 selenium 的以下基本 python 脚本开始:

I started with the following basic python script for using 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),如下所示:

Then, I tried executing it twice (first time without sudo and second time with sudo) as follows:

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'

所以,我必须明白以下几点:

So, I have to understand the following:

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

  1. As observed IOError: [Errno 13] Permission denied: 'geckodriver.log' is being observed when I run the program without a sudo. Is that really required?

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

Even though the browser window is opened, in spite of having resolved all the dependencies (I'm using Ubuntu 16.04) like geckodriver for Firefox, I'm getting the above error KeyError: 'value'. How can I resolve this? Do I have to make any changes in my code to avoid this?

我可以使用一些有用的建议/技巧/调整来让我的程序运行.

I could use some helpful advice/tips/tweaks to get my program running.

PS:深入网络学习 selenium,指出了一些架构特定的问题,如 此处在这里,似乎都没有解决,这让我很担心.chrome 会是更好的选择吗?

P.S.: Digging into the web to learn on selenium, points to some architecture-specific issues as mentioned in here and here and none of them seems to have been solved and that concerns me. Would chrome be a better option?

更新:以下是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<的绝对路径code>firefox 二进制到 firefox_binary.此外,您不需要使用 binary=FirefoxBinary(path_bin).以下代码块在我的 Windows 8 机器上运行良好:

As you were trying to pass a couple of arguments when invoking webdriver.Firefox(), we need to consider that executable_path and firefox_binary both these arguments takes string type of arguments. So you need to pass the absolute path of geckodriver binary to executable_path and the absolute path of firefox binary to firefox_binary. Further you don't need to use binary=FirefoxBinary(path_bin). The following code block works fine on my Windows 8 machine:

代码基于 Python 3.6.1

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

As you are on ubuntu the following code block must work for you:

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 KeyError 'value' 的 Selenium Firefox webdriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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