Proxy proxy = new Proxy();
//设置代理服务器地址
proxy.setHttpProxy("代理服务器地址:端口号");
DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
capabilities.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new HtmlUnitDriver(capabilities)
{
@Override
protected WebClient modifyWebClient(WebClient client)
{
DefaultCredentialsProvider creds = new DefaultCredentialsProvider();
creds.addCredentials("用户名", "密码");
client.setCredentialsProvider(creds);
return client;
}
};
driver.get("http://www.baidu.com");
System.out.println("[" + driver.getTitle() + "]");
//设置代理服务器地址
proxy.setHttpProxy("代理服务器地址:端口号");
DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
capabilities.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new HtmlUnitDriver(capabilities)
{
@Override
protected WebClient modifyWebClient(WebClient client)
{
DefaultCredentialsProvider creds = new DefaultCredentialsProvider();
creds.addCredentials("用户名", "密码");
client.setCredentialsProvider(creds);
return client;
}
};
driver.get("http://www.baidu.com");
System.out.println("[" + driver.getTitle() + "]");