0

我正在尝试使用 selenium python 打开 Tor 浏览器

这是代码

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
import time
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

#path to TOR binary
binary = FirefoxBinary(r'C:\Users\OKILAN\Desktop\Tor 
Browser\Browser\firefox.exe')
#path to TOR profile
profile = FirefoxProfile(r'C:\Users\OKILAN\Desktop\Tor 
Browser\Browser\TorBrowser\Data\Browser\profile.default')
cap = DesiredCapabilities().FIREFOX
cap["marionette"] = False
driver =webdriver.Firefox(capabilities=cap,firefox_binary=binary,executable_path=r'C:\Users\OKILAN\Desktop\geckodriver.exe')
driver.get("http://stackoverflow.com")
time.sleep(5)

driver.quit()

当我运行代码时,它显示 Tor 无法启动,并且出现以下错误

Traceback (most recent call last):
File "C:\Users\OKILAN\Desktop\test1.py", line 13, in <module>
driver = webdriver.Firefox(capabilities=cap,firefox_binary=binary,executable_path=r'C:\Users\OKILAN\Desktop\geckodriver.exe')
File "C:\Users\OKILAN\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 191, in __init__
self.binary, timeout)
File "C:\Users\OKILAN\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\firefox\extension_connection.py", line 52, in __init__
self.binary.launch_browser(self.profile, timeout=timeout)
File "C:\Users\OKILAN\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\firefox\firefox_binary.py", line 73, in launch_browser
self._wait_until_connectable(timeout=timeout)
File "C:\Users\OKILAN\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\firefox\firefox_binary.py", line 104, in _wait_until_connectable
"The browser appears to have exited "
selenium.common.exceptions.WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.

帮我解决这个错误:-(

4

1 回答 1

0

您不需要所有二进制文件和配置文件,创建驱动程序只使用这一行

driver = webdriver.Firefox(executable_path="C:\\geckodriver.exe")
driver.get("http://stackoverflow.com")

确保您已下载并将 geckodriver 设置为环境 1

  1. 首先下载 GeckoDriver for Windows,解压并将路径复制到文件夹。

  2. 右键单击我的电脑或这台电脑。选择属性。

  3. 选择高级系统设置。单击环境变量按钮。
  4. 从系统变量中选择 PATH。点击编辑按钮。单击新建按钮。粘贴 GeckoDriver 文件的路径。
于 2020-04-20T16:24:53.657 回答