如何使用Java的Selenium webdriver为Firefox设置代理?

4
System.setProperty("webdriver.gecko.driver", "E:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
    Proxy p = new Proxy();
    p.setSocksProxy("83.209.94.87:35923");
    DesiredCapabilities cap = new DesiredCapabilities();
    cap.setCapability(CapabilityType.PROXY, p);
    WebDriver driver = new FirefoxDriver(cap);
    driver.get("https://www.google.com.au");

这段代码在主方法中。当我运行此代码时,Firefox被启动,但未跟随Google URL,并且代理未设置为我在以上代码中指定的代理。我该如何解决这个问题?

public static void main(String[] args) throws InterruptedException, IOException, UnsupportedEncodingException {
    while (true) {
    System.setProperty("webdriver.gecko.driver", "E:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
    WebDriver driver;
    String PROXY = "83.209.94.87:35923";
      //Bellow given syntaxes will set browser proxy settings using DesiredCapabilities.
      Proxy proxy = new Proxy();
      proxy.setAutodetect(false);
      proxy.setProxyType(Proxy.ProxyType.MANUAL);
      proxy.setSocksProxy(PROXY);
      DesiredCapabilities cap = new DesiredCapabilities();
      cap.setCapability(CapabilityType.PROXY, proxy);
      //Use Capabilities when launch browser driver Instance.
      driver = new FirefoxDriver(cap);`

Proxy p = new Proxy(); 后面添加 proxy.setProxyType(Proxy.ProxyType.MANUAL);proxy.setAutodetect(false); - Tarun Lalwani
我尝试过这样做,但当我的程序启动Firefox时,代理设置被设置为“使用系统代理设置”。请查看帖子获取新代码。 - user8497118
请再次检查帖子。 - user8497118
目前存在一个开放性的错误,阻止您使用代理 https://github.com/mozilla/geckodriver/issues/764 - Tarun Lalwani
4个回答

4

由于一个漏洞,目前无法使用代理对象。您应该使用以下代码:

    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("network.proxy.type", 1);
    profile.setPreference("network.proxy.socks", "83.209.94.87");
    profile.setPreference("network.proxy.socks_port", 35923);

    FirefoxDriver driver = new FirefoxDriver(profile);
    driver.get("https://www.ipinfo.io");

此错误已在https://github.com/mozilla/geckodriver/issues/764中进行讨论,您可以在下面的链接中看到Marionette驱动程序在后台执行的操作:https://dxr.mozilla.org/mozilla-central/source/testing/marionette/session.js#155。以上代码只是复制相同的内容。

非常感谢!我花了很长时间试图弄清楚为什么我的代码不起作用,但是您提供的解决方案有效! - user8497118
尽管火狐浏览器现在以正确的代理启动,但我无法在该浏览器上访问网站。这很奇怪,因为当我将它设置为手动启动的火狐浏览器时,可以使用相同的代理访问网站。有任何想法为什么会发生这种情况吗? - user8497118
我已经检查过了,对我来说是可以工作的。该IP地址是法国的。 - Tarun Lalwani
从Java Selenium 4.1.1开始,您需要将FirefoxDriver driver = new FirefoxDriver(profile);更改为FirefoxDriver driver = new FirefoxDriver(new FirefoxOptions().setProfile(profile)); - Peter A
我正在使用Java的Selenium 4,但是设置代理地址的这些方法都不起作用。没有错误提示,代理地址就是无法生效。 - Nikki

1

在Selenium 3.14.2、Firefox 62和C# .NET 4.5中工作。

FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(@"GeckoDriver19", "geckodriver.exe");
service.FirefoxBinaryPath = @"C:\Program Files\Mozilla Firefox\firefox.exe";

FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.SetPreference("network.proxy.type", 1);
firefoxOptions.SetPreference("network.proxy.socks", "127.0.0.1");
firefoxOptions.SetPreference("network.proxy.socks_port", 1080);

IWebDriver driver = new FirefoxDriver(service, firefoxOptions);

driver.Navigate().GoToUrl("https://www.hbus.com/register");

1

如上所述,您可以在自动化代码中设置代理详细信息和凭据,但另一种方法是在不与Firefox配置文件共享详细信息的情况下执行它。
Firefox提供了配置文件,我们可以为每个用户创建配置文件并对其进行自定义以用于代理、书签等。
Windows用户:打开运行(win+R)并键入'firefox -p'
Linux用户:运行命令'firefox -p'
1- 它将打开一个对话框,在其中您可以创建自己的配置文件,然后选择该配置文件并打开Firefox。
2- 打开一个新标签页并搜索'about:config'。接受风险并继续,然后单击“显示全部”。
3- 在这里,您可以搜索和设置所有属性。
例如:
network.proxy.type 1
手动为1
自动代理为2
手动代理-
属性                     值
network.proxy.http             您的代理IP
network.proxy.http_port             端口号
network.proxy.ssl             您的代理IP
network.proxy.ssl_port             端口号
network.proxy.ftp             您的代理IP
network.proxy.ftp_port             端口号

(查找您的个人资料名称)
Linux:cd .mozilla/firefox/
Windows:在键盘上按 +R 。会打开一个运行对话框。 输入:%APPDATA%\Mozilla\Firefox\Profiles\ 点击确定。将会打开包含配置文件夹的窗口 现在在Java代码中加载此配置文件

FirefoxOptions options = new FirefoxOptions();
options.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT);
FirefoxProfile profile=new FirefoxProfile(new File("path of your profile"));
options.setProfile(profile);
WebDriver driver = new FirefoxDriver(options);
System.setProperty("webdriver.gecko.driver", "path of gecko driver");
driver.get("url");

-1

使用Java-Selenium 4.1.1版本,以下代码适用于我:

import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

...

Proxy proxy = new Proxy();
proxy.setHttpProxy("myproxy:80");
proxy.setSslProxy("myproxy:80");

FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setProxy(proxy);

WebDriver driver = new FirefoxDriver(firefoxOptions);

如果你喜欢的话,你可以把所有东西都包装在一行里:

WebDriver driver = new FirefoxDriver(new FirefoxOptions().setProxy(new Proxy().setHttpProxy("myproxy:80").setSslProxy("myproxy:80")));

如需更多代理设置,请参阅:Selenium代理API


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接