如何selenium webdriver打开phantomjs
展开全部
//使用phantomjs进行dom、js、canvas、svg等渲染
private WebDriver renderingJS(){
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true); // enabled by default
caps.phantomjs().setBrowserName("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36");
System.out.println(caps.phantomjs().getBrowserName());
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,"F:\\phantomjs\\phantomjs.exe");
PhantomJSDriver webDriver = new PhantomJSDriver(caps);
webDriver.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);
return webDriver;
}
public void executeSubBox(String url){
WebDriver webDriver = renderingJS();
webDriver.get(url);
try {
Thread.sleep(20*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String pageSource = webDriver.getPageSource();
Document document = Jsoup.parse(pageSource);
System.out.println(document);
webDriver.quit();
}
private WebDriver renderingJS(){
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true); // enabled by default
caps.phantomjs().setBrowserName("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36");
System.out.println(caps.phantomjs().getBrowserName());
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,"F:\\phantomjs\\phantomjs.exe");
PhantomJSDriver webDriver = new PhantomJSDriver(caps);
webDriver.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);
return webDriver;
}
public void executeSubBox(String url){
WebDriver webDriver = renderingJS();
webDriver.get(url);
try {
Thread.sleep(20*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String pageSource = webDriver.getPageSource();
Document document = Jsoup.parse(pageSource);
System.out.println(document);
webDriver.quit();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询