![](/img/trans.png)
[英]How to connect to the Tor network using Phantomjs, selenium and c#
[英]C# Selenium - Failed to Start Tor
我正在尝试使用以下代码通过 C# 中的 Selenium 启动 Tor 浏览器:
using OpenQA.Selenium.Firefox;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AutomatorApp
{
public class BrowserAutomator
{
public void Automate()
{
String torPath = "D:\\Tor Browser\\Browser\\firefox.exe";
String profilePath = "D:\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default\\";
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("D:\\geckodriver-v0.26.0-win64", "geckodriver.exe");
firefoxDriverService.FirefoxBinaryPath = torPath;
var firefoxOptions = new FirefoxOptions
{
Profile = profile,
LogLevel = FirefoxDriverLogLevel.Trace
};
FirefoxDriver driver = new FirefoxDriver(firefoxDriverService, firefoxOptions);
}
}
}
但是,这显示了错误“ Tor Failed to Start ”,并且异常只包含“ Permission denied ”。 我尝试以管理员模式启动应用程序并确保所有用户都可以访问该文件夹,但这并没有解决问题。
有趣的是,当我尝试使用相同的设置启动 Firefox 浏览器时,它运行良好。
很感谢任何形式的帮助。
更新 -已解决:更新到最新的 Tor 版本(9.5.1)后,最终的工作代码:
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");
重要笔记:
需要在about:config中更改以下 TOR 配置:
marionette.enabled : 真
marionette.port :设置为未使用的端口,并在代码中将此值设置为firefoxDriverService.BrowserCommunicationPort 。 在我的示例中设置为 2828。
据我几年前的尝试记忆,当您设置“ Profile
”选项时,带有 WebDriver 的 TOR 不起作用。 正常 Firefox 工作,但 TOR 根本没有。
如果在此之后出现超时错误,请确保在about:config
上启用了marionette
。 如果它已经启用,请按照启动时 TOR 的情况进行操作。 就像实际的 firefox 浏览器无法加载,卡在连接启动器等,或者在浏览器启动时出现消息框等等......并且还要确保没有其他firefox.exe
, geckodriver.exe
或tor.exe
在后台运行。 如果多个可执行文件未配置为侦听不同的端口号,则可能会导致问题。
Environment.SetEnvironmentVariable("webdriver.gecko.driver", "C:\\TorBrowser\\Browser\\geckodriver.exe");
var gekcoService = FirefoxDriverService.CreateDefaultService("C:\\TorBrowser\\Browser", "geckodriver.exe");
var gekcoService.FirefoxBinaryPath = "C:\\TorBrowser\\Browser\\firefox.exe";
// marionette port that browser listens to. I had it set to a custom port on about:config
var gekcoService.BrowserCommunicationPort = 50111;
// also had given a custom port for geckodriver listen port
var gekcoService.Port = 9881;
var gekcoService.Host = 127.0.0.1;
var gekcoService.HostName = 127.0.0.1;
var gekcoService.Start();
var ffOptions = new FirefoxOptions
{
AcceptInsecureCertificates = true,
BrowserExecutableLocation = "C:\\TorBrowser\\Browser\\firefox.exe",
Profile = null,
UnhandledPromptBehavior = UnhandledPromptBehavior.Dismiss
};
ffOptions.AddArguments("-profile", "C:\\TorBrowser\\Browser\\TorBrowser\\Data\\Browser\\profile.default");
ffOptions.LogLevel = FirefoxDriverLogLevel.Info;
ffOptions.PageLoadStrategy = PageLoadStrategy.Eager;
var ffDriver = new FirefoxDriver(gekcoService, ffOptions);
ffDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(10);
ffDriver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(10);
ffDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
您的代码块对我来说看起来很完美。
但是,打开使用默认Firefox v68.9.0esr的Tor Browser 9.5似乎存在问题。
解决方案是安装和使用以下任一浏览器:
如果您使用上述浏览器,您需要:
Firefox v77.0.1 :
String torPath = "C:\\Program Files\\Mozilla Firefox\\firefox.exe";
Firefox 夜间版 v79.0a1
String torPath = "C:\\Program Files\\Firefox Nightly\\firefox.exe";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.