本文整理汇总了Java中com.gargoylesoftware.htmlunit.WebClient.waitForBackgroundJavaScript方法的典型用法代码示例。如果您正苦于以下问题:Java WebClient.waitForBackgroundJavaScript方法的具体用法?Java WebClient.waitForBackgroundJavaScript怎么用?Java WebClient.waitForBackgroundJavaScript使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.gargoylesoftware.htmlunit.WebClient
的用法示例。
在下文中一共展示了WebClient.waitForBackgroundJavaScript方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import com.gargoylesoftware.htmlunit.WebClient; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
// 浏览器
WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setUseInsecureSSL(true);//支持https
webClient.getOptions().setJavaScriptEnabled(true); // 启用JS解释器,默认为true
webClient.getOptions().setCssEnabled(false); // 禁用css支持
webClient.getOptions().setThrowExceptionOnScriptError(false); // js运行错误时,是否抛出异常
webClient.getOptions().setTimeout(10000); // 设置连接超时时间 ,这里是10S。如果为0,则无限期等待
webClient.getOptions().setDoNotTrackEnabled(false);
webClient.setJavaScriptTimeout(8000);//设置js运行超时时间
webClient.waitForBackgroundJavaScript(500);//设置页面等待js响应时间,
// proxy
//webClient.getOptions().setProxyConfig(new ProxyConfig("IP", 80));
HtmlPage page = webClient.getPage("http://---");
String pageXml = page.asXml(); //以xml的形式获取响应文本
System.out.println(pageXml);
}
示例2: completeCloudflareBrowserCheck
import com.gargoylesoftware.htmlunit.WebClient; //导入方法依赖的package包/类
public static Cookie completeCloudflareBrowserCheck(final String url) {
WebClient completeClient = new WebClient(BrowserVersion.CHROME);
completeClient.getOptions().setCssEnabled(false);
completeClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
final HtmlPage page;
final HtmlElement submitButton;
final HtmlForm challengeForm;
try {
page = completeClient.getPage(url);
completeClient.waitForBackgroundJavaScript(5000);
submitButton = (HtmlElement) page.createElement("button");
submitButton.setAttribute("type", "submit");
challengeForm = (HtmlForm) page.getElementById("challenge-form");
challengeForm.appendChild(submitButton);
submitButton.click();
return completeClient.getCookieManager().getCookie("cf_clearance");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
示例3: create
import com.gargoylesoftware.htmlunit.WebClient; //导入方法依赖的package包/类
public static WebClient create(String host,int port) {
LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log",
"org.apache.commons.logging.impl.NoOpLog");
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(
Level.OFF);
java.util.logging.Logger.getLogger("org.apache.http.client").setLevel(
Level.OFF);
// LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log","org.apache.commons.logging.impl.NoOpLog");
// java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17,host,port);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setCssEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getOptions().setTimeout(60000);
webClient.setJavaScriptTimeout(60000);
webClient.waitForBackgroundJavaScript(120000);
return webClient;
}
示例4: main
import com.gargoylesoftware.htmlunit.WebClient; //导入方法依赖的package包/类
public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
// 屏蔽HtmlUnit等系统 log
LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log","org.apache.commons.logging.impl.NoOpLog");
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
java.util.logging.Logger.getLogger("org.apache.http.client").setLevel(Level.OFF);
String url = "https://www.douyin.com/share/video/6496703951436516621/?mid=6484356820260686606";
System.out.println("Loading page now-----------------------------------------------: "+url);
/* HtmlUnit 模拟浏览器 */
WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setJavaScriptEnabled(true); // 启用JS解释器,默认为true
webClient.getOptions().setCssEnabled(false); // 禁用css支持
webClient.getOptions().setThrowExceptionOnScriptError(false); // js运行错误时,是否抛出异常
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setTimeout(10 * 1000); // 设置连接超时时间
HtmlPage page = webClient.getPage(url);
webClient.waitForBackgroundJavaScript(30 * 1000); // 等待js后台执行30秒
String pageAsXml = page.asXml();
/* Jsoup解析处理 */
// Document doc = Jsoup.parse(pageAsXml, "https://bluetata.com/");
Document doc = Jsoup.parse(pageAsXml);
Elements pngs = doc.select("img[src$=.png]"); // 获取所有图片元素集
// 其他操作
System.out.println(doc.toString());
}