【问题标题】:Selenium: changing proxy in FirefoxSelenium:在 Firefox 中更改代理
【发布时间】:2018-08-28 02:46:54
【问题描述】:

我正在用 selenium 编写测试,并且想在 Firefox 中将代理更改为自动检测,默认是系统设置中的代理。怎么做?

我的代码如下:

public class SodirRejestracja {
String baseUrl = "http://google.pl"; 
String driverPath= "C:\\geckodriver.exe";
WebDriver driver;


@BeforeTest
public void beforeTest() {
    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("network.proxy.type", 2);
    System.setProperty("webdriver.gecko.driver", driverPath);
    driver=new FirefoxDriver(profile);
}
@Test
public void test(){
    driver.get("http://google.com");
}
}

以上代码来自How do I set a proxy for firefox using Selenium webdriver with Java?

但在 driver=new FirefoxDriver(profile) 行中,我得到:“构造函数 FirefoxDriver(FirefoxProfile) 未定义”

【问题讨论】:

    标签: java selenium gecko


    【解决方案1】:

    一个应该做的样本(未经测试但可以编译)

    String proxyName = <yourProxyHost> + ":" + <yourProxyPort>;
    Proxy proxy = new Proxy();
    proxy.setHttpProxy(proxyName)
         .setFtpProxy(proxyName)
         .setSslProxy(proxyName);
    
    DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox();
    desiredCapabilities.setCapability(CapabilityType.PROXY, proxy);
    FirefoxOptions options = new FirefoxOptions(desiredCapabilities);
    driver = new FirefoxDriver(options);
    

    【讨论】:

    • 好的,我明天检查一下,但我不需要设置任何代理名称等,只需单击“自动检测”单选按钮
    • 那么可能是 proxy.setAutodetect(true);
    【解决方案2】:

    此代码有效

    Proxy proxy = new Proxy();
        proxy.setProxyType(Proxy.ProxyType.AUTODETECT);
        FirefoxOptions options = new FirefoxOptions();
        options.setProxy(proxy);
        driver = new FirefoxDriver(options);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-19
      • 2021-10-12
      • 1970-01-01
      • 2013-09-14
      • 2022-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多