我当时看着this excellent answer,并复制了MacOS的代码(答案的顶部,为方便起见,也在下面复制)。
import os # Code from answer linked above
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium import webdriver
# path to the firefox binary inside the Tor package
binary = '/Applications/TorBrowser.app/Contents/MacOS/firefox'
if os.path.exists(binary) is False:
raise ValueError("The binary path to Tor firefox does not exist.")
firefox_binary = FirefoxBinary(binary)
browser = None
def get_browser(binary=None):
global browser
# only one instance of a browser opens, remove global for multiple instances
if not browser:
browser = webdriver.Firefox(firefox_binary=binary)
return browser
if __name__ == "__main__":
browser = get_browser(binary=firefox_binary)
urls = (
('tor browser check', 'https://check.torproject.org/'),
('ip checker', 'http://icanhazip.com')
)
for url_name, url in urls:
print "getting", url_name, "at", url
browser.get(url)
我一直收到以下错误。
Traceback (most recent call last):
File "torselenium.py", line 22, in <module>
browser = get_browser(binary=firefox_binary)
File "torselenium.py", line 18, in get_browser
browser = webdriver.Firefox(firefox_binary=binary)
File "/Users/user/Library/Python/2.7/lib/python/site-packages/selenium/webdriver/firefox/webdriver.py", line 164, in __init__
self.service.start()
File "/Users/user/Library/Python/2.7/lib/python/site-packages/selenium/webdriver/common/service.py", line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
此错误与this question中描述的错误非常相似,此处建议设置firefox_binary
路径。但是,我已经做到了这一点,并验证(在下面的验证证明中)该路径上存在一个firefox二进制文件。
$ ls /Applications/TorBrowser.app/Contents/MacOS/firefox
/Applications/TorBrowser.app/Contents/MacOS/firefox
实际上,如果我将二进制路径(第7行)设置为'~/Applications/TorBrowser.app/Contents/MacOS/firefox'
,它就会给我ValueError
,因此肯定有二进制文件。
我也checked if I have execute permissions(我也这样做了,我尝试将browser
从全局变量更改为局部变量,无济于事。
如何解决此错误?
答案 0 :(得分:0)
您似乎已经足够亲密。使用selenium3通过Selenium驱动的GeckoDriver启动 Mozilla Firefox 浏览上下文,您必须下载最新的GeckoDriver v0.26.0并将其存储在系统中,在启动 Web浏览器会话时,请提及绝对路径,如下所示:
browser = webdriver.Firefox(firefox_binary=binary, executable_path='/usr/local/bin/geckodriver')
中找到详细的讨论
您可以在 WebDriverException上找到一些相关的讨论:'geckodriver'可执行文件必须位于PATH 中,错误在于: