本文整理匯總了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());
}