如果您的目標是在不同的IP地址上測試您的功能,您可以使用Tor瀏覽器。
public IWebDriver Driver { get; set; }
public Process TorProcess { get; set; }
public WebDriverWait Wait { get; set; }
[TestInitialize]
public void SetupTest()
{
String torBinaryPath = @"C:\Users\aangelov\Desktop\Tor Browser\Browser\firefox.exe";
this.TorProcess = new Process();
this.TorProcess.StartInfo.FileName = torBinaryPath;
this.TorProcess.StartInfo.Arguments = "-n";
this.TorProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
this.TorProcess.Start();
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("network.proxy.type", 1);
profile.SetPreference("network.proxy.socks", "127.0.0.1");
profile.SetPreference("network.proxy.socks_port", 9150);
this.Driver = new FirefoxDriver(profile);
this.Wait = new WebDriverWait(this.Driver, TimeSpan.FromSeconds(60));
}
[TestCleanup]
public void TeardownTest()
{
this.Driver.Quit();
this.TorProcess.Kill();
}
這裏是刷新Tor身份的代碼。
public void RefreshTorIdentity()
{
Socket server = null;
try
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9151);
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Connect(ip);
server.Send(Encoding.ASCII.GetBytes("AUTHENTICATE \"johnsmith\"" + Environment.NewLine));
byte[] data = new byte[1024];
int receivedDataLength = server.Receive(data);
string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
server.Send(Encoding.ASCII.GetBytes("SIGNAL NEWNYM" + Environment.NewLine));
data = new byte[1024];
receivedDataLength = server.Receive(data);
stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
if (!stringData.Contains("250"))
{
Console.WriteLine("Unable to signal new user to server.");
server.Shutdown(SocketShutdown.Both);
server.Close();
}
}
finally
{
server.Close();
}
}
你可以在這裏找到更詳細的信息:http://automatetheplanet.com/using-selenium-webdriver-tor-c-code/
的代碼示例在C#中,但代碼應該在Java中是相同的。
您好,感謝您的答覆,但FirefoxProfile.SetProxyPreferences()被棄用v2.40.0的:http://grepcode.com/file_/repo1 .maven.org/maven2/org.seleniumhq.selenium/selenium-firefox-driver/2.45.0/org/openqa/selenium/firefox/FirefoxProfile.java /?v = diff&id2 = 2.40.0 – AndreiB
然後應該有類似的實現。那是什麼? – Saifur