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

2024-05-12 15:17:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图通过tor Browser 9.5启动tor浏览会话,它使用默认的Firefox v68.9.0esr系统上通过Python使用GeckoDriverSelenium。但我面临一个错误:

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每晚使用各自的二进制文件工作

我需要其他设置吗?有人能帮我吗


Firefox快照

tor_firefox


Firefox夜间快照

tor_firefoxNightly


Tags: importbrowserusernamenetworkfirefoxprofileuserstor
2条回答

我通过更新到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配置:

  • 木偶。已启用:真

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

注意
我不确定这是否是确切的答案(因此,我非常感谢反馈)

解决方案
我成功地向check-tor页面(https://check.torproject.org/)发送了一个get请求,它向我显示了一个未知的IP(另外,如果您在一段时间后重复该请求,则IP会有所不同)

基本上,我已经设置了chrome驱动程序来运行TOR。代码如下:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

tor_proxy = "127.0.0.1:9150"

chrome_options = Options()

chrome_options.add_argument(" test-type")
chrome_options.add_argument(' ignore-certificate-errors')
chrome_options.add_argument(' disable-extensions')
chrome_options.add_argument('disable-infobars')
chrome_options.add_argument(" incognito")
chrome_options.add_argument(' proxy-server=socks5://%s' % tor_proxy)

driver = webdriver.Chrome(options=chrome_options)


driver.get('https://check.torproject.org/')

因为驱动程序没有处于headless模式,所以您可以自己检查生成的页面。它应该是:
祝贺您。此浏览器已配置为使用Tor。[IP信息]。但是,它似乎不是Tor浏览器。单击此处转到下载页面“

确保chromedriver.exe文件链接在路径上,或提供文件路径作为driver.Chrome()函数的参数

编辑:确保TOR浏览器在后台运行,感谢@Abhishek Rai指出这一点

相关问题 更多 >