[英]Is there a way to trigger scroll event with HtmlUnit or is it not possible at all?
我目前正在學習HtmlUnit以便抓取網站。 一切順利,直到我遇到動態頁面(例如,我正在使用Pinterest網站),當用戶向下滾動時,動態添加元素。
我已經嘗試了幾種應該在真實瀏覽器中觸發滾動的方法(我將在下面顯示)。 在進一步討論之前,我想提一下我已經設置了以下配置:
webclient.setJavaScriptEnabled(true);
webclient.setAjaxController(new NicelyResynchronizingAjaxController());
讓我們說我想讓所有關注者都在Pinterest上。 我導航到那個頁面,現在從第一次只有24個,我想向下滾動,觸發Ajax調用服務器並檢索下一組關注者的事件。
1)簡單的javascript或jQuery代碼觸發窗口滾動。
ScriptResult sr = followersPage.executeJavaScript("window.scrollBy(0,1000)");
// One version in jQuery
// ScriptResult sr = followersPage.executeJavaScript("$(window).scrollTop(0,1000);");
// also tried with the body, html, with animation
// ScriptResult sr = followersPage.executeJavaScript("$("html, body").animate({ scrollTop: $(document).height() }, 1000);");
webclient.waitForBackgroundJavaScript(10000);
followersPage = (HtmlPage)sr.getNewPage();
=>當我檢查到頂部的距離時,它等於0,結果頁面與原始頁面相同。 在Eclipse中進行調試時,當我跨越執行javascript的行時,它會直接轉到下一行,沒有任何延遲。 如果我寫任何其他JavaScript,例如:
ScriptResult sr = followersPage.executeJavaScript("$(div.GridItems).html('new content')");
您可以注意到調試器在該行上掛起了半秒鍾,這意味着執行了javascript。
2)將焦點從一個跟隨錨更改為另一個(我選擇了錨,因為當您單擊TAB鍵時它在焦點順序中使用):
HtmlDivision gridItems = followersPage.getFirstByXPath("//div[contains(concat(' ',@class,' '),' GridItems ')]");
List<HtmlDivision> els = (List<HtmlDivision>) gridItems.getByXPath("//div[@class='item ']");
List<HtmlDivision> items = (List<HtmlDivision>) gridItems.getByXPath("//div[@class='item ']");
for (HtmlDivision item : items) {
HtmlAnchor a = item.getFirstByXPath("//a[@class='userWrapper']");
a.focus();
webClient.waitForBackgroundJavaScript(1000);
}
followersPage = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();
同樣,沒有滾動發生。 結果頁面與原始頁面保持一致
3)創建一個觸發窗口滾動事件的按鈕:
HtmlButton scrollButton = (HtmlButton) followersPage.createElement("button");
scrollButton.setAttribute("type", "button");
scrollButton.setAttribute("onclick", "window.scrollTo(0,document.body.scrollHeight);");
gridItems.appendChild(scrollButton);
followersPage = scrollButton.click();
不幸的是,它沒有用。
我嘗試了很多其他方法但直到現在都沒有積極的結果。
我在這里閱讀了很多相關文章,也就是關於該主題的stackoverflow。 似乎沒有人設法使用HtmlUnit進行滾動工作,因為大多數問題仍未得到答復。 這就是為什么我想知道這個功能是否有用。
有人設法滾動頁面(簡單頁面,沒有ajax)? 有人設法滾動頁面,事件觸發了一些ajax調用嗎?
我建議你在這種情況下使用casperjs而不是htmlunit,我試過用htmlunit打開pinterest並得到了
runtimeError: message=[Property 0 not found.] sourceName=[https://s.pinimg.com/webapp/js/vendor-react-d20f99c48b5d58e4821c.js] line=[1] lineSource=[null] lineOffset=[0]
所以它確實看起來htmlunit沒有對js的良好支持,即使是最新版本2.31 ..
這是一個使用casperjs的演示代碼:
var utils = require('utils')
var fs = require('fs')
var system = require('system')
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
localToRemoteUrlAccessEnabled: true,
webSecurityEnabled: false,
plainTextAllContent: false,
viewportSize: {
width: 1440,
height: 800
},
onError: function(casper, msg, backtrace) {
utils.dump(backtrace)
}
});
var cookie = fs.read('cookie.txt').trim()
casper.on('started', function() {
this.page.onError = function(msg, trace) {
casper.echo('Error => ' + msg + '\nError trace => ')
utils.dump(trace)
}
this.page.customHeaders = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "zh-CN,en;q=0.5",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"Pragma": "no-cache",
"Cookie": cookie
}
});
casper.start('https://www.pinterest.com', function() {
this.then(function() {
this.waitForSelector('div[class="_wx _2h"]', function() {
this.echo("waitForSelector 'div[class=_wx _2h]' is done")
this.scrollTo(0, 1000);
this.wait(5000, function() {
this.scrollTo(0, 2000);
})
})
})
});
將上面的代碼保存到名為demo.js的文件中,然后使用以下命令啟動casperjs
casperjs --engine=slimerjs demo.js
然后你會看到firefox瀏覽器啟動視覺和工作!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.