我从以下基本 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'
所以,我必须了解以下内容:
据观察,当我在没有
sudo
的情况下运行程序时,观察到IOError: [Errno 13] Permission returned: 'geckodriver.log'
。这真的是必需的吗?即使打开了浏览器窗口,尽管已经解决了所有依赖项(我使用的是 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_path
和 firefox_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/