Selenium:在Firefox中更改代理

时间:2018-03-19 13:54:32

标签: java selenium gecko

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

2 个答案:

答案 0 :(得分:0)

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

Stop-AzureVM : The term 'Stop-AzureVM' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling of 
the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Stop-AzureVM -ResourceGroupName <resourceGroup>-Name <vmName>
+ ~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (Stop-AzureVM:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

答案 1 :(得分:0)

此代码有效

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