Selenium:在Firefox中更改代理

我正在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)未定义”

回答(1)

3 years ago

应该这样做的样本(未经测试但编译)

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);