问题描述
我正在尝试通过 Tor 浏览器 9.5 启动 Tor 浏览会话,该浏览器使用默认的 Firefox v68.9.0esr 和
代码块:
from selenium import webdriver从 selenium.webdriver.firefox.firefox_profile 导入 FirefoxProfile导入操作系统torexe = os.popen(r'C:UsersusernameDesktopTor BrowserBrowserTorBrowserTor or.exe')profile = FirefoxProfile(r'C:UsersusernameDesktopTor BrowserBrowserTorBrowserDataBrowserprofile.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:UsersusernameDesktopTor BrowserBrowserfirefox.exe'driver = webdriver.Firefox(firefox_profile= profile, options = firefox_options, executable_path=r'C:WebDriversgeckodriver.exe')driver.get("https://www.tiktok.com/")
相同的代码块可以通过 Firefox 和 Firefox Nightly 使用各自的二进制文件运行.
我需要任何额外的设置吗?有人可以帮我吗?
Firefox 快照:
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 = 新的 FirefoxOptions{个人资料 = 空,LogLevel = FirefoxDriverLogLevel.Trace};firefoxOptions.AddArguments(-profile", profilePath);FirefoxDriver 驱动程序 = 新的 FirefoxDriver(firefoxDriverService, firefoxOptions);driver.Navigate().GoToUrl(https://www.google.com");
重要说明:
about:config 中需要更改以下 TOR 配置:
marionette.enabled:真实
marionette.port:设置为未使用的端口,并在您的代码中将此值设置为 firefoxDriverService.BrowserCommunicationPort.在我的示例中设置为 2828.
I'm trying to initiate a tor browsing session through Tor Browser 9.5 which uses the default Firefox v68.9.0esr using GeckoDriver and Selenium through Python on a windows-10 system. But I'm facing an error as:
Code Block:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
import os
torexe = os.popen(r'C:UsersusernameDesktopTor BrowserBrowserTorBrowserTor or.exe')
profile = FirefoxProfile(r'C:UsersusernameDesktopTor BrowserBrowserTorBrowserDataBrowserprofile.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:UsersusernameDesktopTor BrowserBrowserfirefox.exe'
driver = webdriver.Firefox(firefox_profile= profile, options = firefox_options, executable_path=r'C:WebDriversgeckodriver.exe')
driver.get("https://www.tiktok.com/")
Where as the same code block works through Firefox and Firefox Nightly using the respective binaries.
Do I need any additional settings? Can someone help me out?
Firefox Snapshot:
Firefox Nightly Snapshot:
I managed to resolve this by updating to v9.5.1 and implementing the following changes:
Note that although the code is in C# the same changes to the Tor browser and how it is launched should be applied.
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");
Important notes:
The following TOR configs need to be changed in about:config :
marionette.enabled: true
marionette.port: set to an unused port, and set this value to firefoxDriverService.BrowserCommunicationPort in your code. This was set to 2828 in my example.
这篇关于如何使用 GeckoDriver 和 Selenium 通过 Python 启动使用默认 Firefox 到 68.9.0esr 的 Tor 浏览器 9.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!