用com.codeborne.phantomjsdriver解决了我的情况注意:它是groovy代码。
的pom.xml
com.codeborne
phantomjsdriver
PhantomJsUtils.groovyimport org.jsoup.Jsoupimport org.jsoup.nodes.Documentimport org.openqa.selenium.WebDriverimport org.openqa.selenium.phantomjs.PhantomJSDriverclass PhantomJsUtils {
private static String filePath = 'data/temp/';
public static Document renderPage(String filePath) {
System.setProperty("phantomjs.binary.path", 'libs/phantomjs') // path to bin file. NOTE: platform dependent
WebDriver ghostDriver = new PhantomJSDriver();
try {
ghostDriver.get(filePath);
return Jsoup.parse(ghostDriver.getPageSource());
} finally {
ghostDriver.quit();
}
}
public static Document renderPage(Document doc) {
String tmpFileName = "$filePath${Calendar.getInstance().timeInMillis}.html";
FileUtils.writeToFile(tmpFileName, doc.toString());
return renderPage(tmpFileName);
}}
ClassInProject.groovyDocument doc = PhantomJsUtils.renderPage(Jsoup.parse(yourSource))