Selenium WebDriver + Java - 如何为 Firefox 配置代理设置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30301117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium WebDriver + Java - How to configure proxy settings for Firefox?
提问by AndreiB
I'm a rookie test developer using selenium 2.45 and I'm trying to configure my FirefoxDriver to use my company's proxy settings. I am failing to do so :)
我是一名使用 selenium 2.45 的新手测试开发人员,我正在尝试配置我的 FirefoxDriver 以使用我公司的代理设置。我没有这样做:)
I am following the instruction from here to create a profile on-the-fly:
我正在按照此处的说明即时创建配置文件:
My code looks like this:
我的代码如下所示:
public static WebDriver driver;
String usedProxy = "http://myproxy:8080";
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(usedProxy).setFtpProxy(usedProxy).setSslProxy(usedProxy);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);
driver = new FirefoxDriver(cap);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://TestWebsite.com");
I am not receiving any kind of errors but the connection is not working for this browser. When checking Options>Advanced>Network>Connection Settings from Firefox menu, proxy is set to manual but text inputs only contain "http://"
我没有收到任何类型的错误,但该浏览器的连接不起作用。从 Firefox 菜单检查选项>高级>网络>连接设置时,代理设置为手动但文本输入仅包含“http://”
PS: I have a feeling this is relevant but am not sure: TestWebsite.com will load via https only (it's a shopping cart)
PS:我觉得这是相关的,但我不确定:TestWebsite.com 将仅通过 https 加载(这是一个购物车)
回答by Anton Angelov
If your goal is to test your functionality on different IP addresses, you can use the Tor Browser.
如果您的目标是在不同的 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();
}
Here is the code to refresh the Tor identity.
这是刷新 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();
}
}
You can find more detailed information here: http://automatetheplanet.com/using-selenium-webdriver-tor-c-code/
您可以在此处找到更多详细信息:http: //automatetheplanet.com/using-selenium-webdriver-tor-c-code/
The code examples are in C# but the code should be identical in Java.
代码示例在 C# 中,但代码在 Java 中应该是相同的。
回答by Saifur
Try firefox profile as well. Note this is C# code and converting to Java
should be fairly simply
也可以尝试 Firefox 配置文件。请注意,这是 C# 代码,转换为Java
应该相当简单
string usedProxy = "http://myproxy:8080";
Proxy proxy = new OpenQA.Selenium.Proxy();
proxy.HttpProxy = usedProxy;
proxy.FtpProxy = usedProxy;
proxy.SslProxy = usedProxy;
FirefoxProfile profile = new FirefoxProfile();
profile.SetProxyPreferences(proxy);
回答by shiuu
This code works for me:
这段代码对我有用:
FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", <proxy_url>);
profile.setPreference("network.proxy.http_port", 8080);
profile.setPreference("network.proxy.ssl", <proxy_url>);
profile.setPreference("network.proxy.ssl_port", 8080);
options.setProfile(profile);
options.setAcceptInsecureCerts(true)
options.setCapability("disable-restore-session-state", true);
options.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(options);
I followed answers from this link: Webdriver and proxy server for firefox
我按照此链接的答案进行操作:Webdriver and proxy server for firefox