python - 如何通过 Python 使用 GeckoDriver 和 Selenium 启动使用默认 Firefox 到 68.9.0esr 的 Tor 浏览器 9.5

标签 python selenium firefox geckodriver tor

我正在尝试通过 Tor Browser 9.5 启动一个 Tor 浏览 session ,它使用默认的 Firefox v68.9.0esr 使用 GeckoDriverSelenium 上通过 Python系统。但我面临一个错误:
tor_firefoxESR
代码块:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
import os

torexe = os.popen(r'C:\Users\username\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
profile = FirefoxProfile(r'C:\Users\username\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default')
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9050)
profile.set_preference("network.proxy.socks_remote_dns", False)
profile.update_preferences()
firefox_options = webdriver.FirefoxOptions()
firefox_options.binary_location = r'C:\Users\username\Desktop\Tor Browser\Browser\firefox.exe'
driver = webdriver.Firefox(firefox_profile= profile, options = firefox_options, executable_path=r'C:\WebDrivers\geckodriver.exe')
driver.get("https://www.tiktok.com/")
相同的代码块在 Firefox 和 Firefox Nightly 中使用各自的二进制文件。
我需要任何其他设置吗?有人可以帮我吗?

火狐快照 :
tor_firefox

Firefox 夜间快照 :
tor_firefoxNightly

最佳答案

我设法通过更新到 v9.5.1 并实现以下更改来解决此问题:
请注意,尽管代码是在 C# 中,但应该应用对 Tor 浏览器及其启动方式的相同更改。

FirefoxProfile profile = new FirefoxProfile(profilePath);
profile.SetPreference("network.proxy.type", 1);
profile.SetPreference("network.proxy.socks", "127.0.0.1");
profile.SetPreference("network.proxy.socks_port", 9153);
profile.SetPreference("network.proxy.socks_remote_dns", false);

FirefoxDriverService firefoxDriverService = FirefoxDriverService.CreateDefaultService(geckoDriverDirectory);
firefoxDriverService.FirefoxBinaryPath = torPath;
firefoxDriverService.BrowserCommunicationPort = 2828;
var firefoxOptions = new FirefoxOptions
{
    Profile = null,
    LogLevel = FirefoxDriverLogLevel.Trace
};
firefoxOptions.AddArguments("-profile", profilePath);
FirefoxDriver driver = new FirefoxDriver(firefoxDriverService, firefoxOptions);
driver.Navigate().GoToUrl("https://www.google.com");
重要笔记:
中需要更改以下 TOR 配置关于:配置 :
  • marionette.enabled : 真的
  • 木偶.port : 设置为未使用的端口,并将此值设置为 firefoxDriverService.BrowserCommunicationPort 在你的代码中。在我的示例中设置为 2828。
  • 关于python - 如何通过 Python 使用 GeckoDriver 和 Selenium 启动使用默认 Firefox 到 68.9.0esr 的 Tor 浏览器 9.5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62666075/

    相关文章:

    python - 我应该检查构造函数参数的类型(以及其他地方)吗?

    python - 无法在 scrapy 脚本中禁用日志消息

    javascript - 如何使用 ActionSequence 或 LegacyActionSequence 或类似的方法在 Javascript 中链接操作?

    java - 有没有办法在一定时间内错开 TestNG 测试?如果是这样,如何?

    html - SVG 在 Firefox 中的呈现方式有所不同

    CSS 在 Firefox 2.0 中不工作

    java - selenium 可以处理自动完成吗?

    c# - 具有返回值和输出参数的 Python NET 调用 C# 方法

    python - 如何在 Django 中显示每个用户的特定付款?

    java - 带有页面对象模型页面工厂的 Selenium 网格