我一直想知道在我自己的研究过程之后,在java中的selenium webdriver中为phantomJs提供代理凭证的方法我觉得它已经完成了但是我还在接收 407 (代理问题)我已提供下面的代码供您审核,帮助会很大程度上适用
public class PhantomProxy {
private static PhantomJSDriverService service;
private static WebDriver webDriver;
protected static DesiredCapabilities dCaps;
public static void main(String[] args) {
String[]a={"--proxy=ip:10....(your proxy ip)","--proxy-auth=username:password"};
service = new PhantomJSDriverService.Builder()
.usingPhantomJSExecutable(new File("your own custom path\\phantomjs.exe"))
.usingCommandLineArguments(a).usingPort(6050).build();
service.start();
Capabilities caps = new DesiredCapabilities();
((DesiredCapabilities) caps).setJavascriptEnabled(true);
((DesiredCapabilities) caps).setCapability("takesScreenshot", true);
webDriver = new RemoteWebDriver(service.getUrl(), caps);
webDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
webDriver.get("http://www.google.com");
Thread.sleep(2000L);
String pageSource=webDriver.getPageSource();
System.out.println(pageSource);
webDriver.quit();
}
需要注意的是命令行参数在命令行界面中工作正常,但我完全不知道为什么它不能在这里工作
答案 0 :(得分:0)
这是代码的一部分,适用于Windows上的phantomJs 2.0.0
private static void phantomJsDriver() {
final String pathToPhantom = phantomJsPath();
DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, pathToPhantom);
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCapabilities());
driver = new PhantomJSDriver(capabilities);
}
private static List<String> cliArgsCapabilities() {
List<String> cliArgsCap = new ArrayList<String>();
cliArgsCap.add("--proxy=url:port");
cliArgsCap.add("--proxy-auth=user:pass");
cliArgsCap.add("--proxy-type=http");
return cliArgsCap;
}
您只需要使用自己的代理值
更改行cliArgsCap.add("--proxy=url:port");
cliArgsCap.add("--proxy-auth=user:pass");