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

2023-12-20

我正在尝试通过以下方式启动 Tor 浏览会话托尔浏览器 9.5它使用默认的火狐浏览器 v68.9.0esr using Gecko驱动程序 https://stackoverflow.com/questions/43660195/why-firefox-requires-geckodriver/43661697#43661697 and Selenium https://stackoverflow.com/questions/54459701/what-is-selenium-and-what-is-webdriver/54482491#54482491通过Python在视窗10 /questions/tagged/windows-10系统。但我面临一个错误:

代码块:

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 运行。

我需要任何额外的设置吗?有人可以帮我吗?


火狐快照:


Firefox 夜间快照:


我设法通过更新到 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 配置需要更改关于:配置 :

  • 启用木偶: true

  • 木偶.port:设置为未使用的端口,并将该值设置为firefoxDriverService.BrowserCommunicationPort在你的代码中。在我的示例中,该值设置为 2828。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐