How to read a page with an infinite scroll bar using selenium's html unit driver for java? -
for example, if wanted find post on facebook year ago using selenium how able scroll down , text. have figured out how scroll using selenium whenever try elements or page source includes initial loaded page, nothing scrolled down to. i'm not using facebook, i'm using website doesn't have java developer tools, stock twits.
the logic followed in example find post text content
let allposts while timeout visible posts has text in remove allposts currentposts [so dont need check same post again] , add currentposts allposts[to maintain list] each post in currentposts check if post's text contains given text stop scroll bottom[which invokes ajax call load more posts] //replace above button loadmore or if scroll dint invoke ajax load wait till page loaded again
this worked me found post on wall on birthday[1 month ago].
it took 20 mins [depends on no of posts , time of post take more time]
the following search facebook news feed given text
public static void fbsearch() { system.setproperty("webdriver.chrome.driver", "d:\\galen\\chromedriver.exe"); webdriver driver = new chromedriver(); driver.get("http://www.facebook.com"); driver.findelement(by.name("email")).sendkeys("phystem"); driver.findelement(by.name("pass")).sendkeys("yyy"); driver.findelement(by.id("loginbutton")).click(); waitforpageloaded(driver); fbpostsearch(driver, "true story", 20);//timeout in mins } public static boolean fbpostsearch(webdriver driver, string postcontent, int timeoutinmins) { set<webelement> allposts = new hashset<>(); int totaltime = timeoutinmins * 60000; // in millseconds long starttime = system.currenttimemillis(); boolean timeends = false; while (!timeends) { list<webelement> posts = getposts(driver); posts.removeall(allposts);//to remove old posts searched allposts.addall(posts);//append new posts posts (webelement post : posts) { string content = post.gettext(); if (content.contains(postcontent)) { //this our element system.out.println("found"); new actions(driver).movetoelement(post).build().perform(); ((javascriptexecutor) driver).executescript("arguments[0].style.outline='2px solid #ff0';", post); return true; } } scrolltobottom(driver); waitforpageloaded(driver); timeends = (system.currenttimemillis() - starttime >= totaltime); } system.out.println("not found"); return false; } public static list<webelement> getposts(webdriver driver) { //finding posts has textcontent coz posts image return driver.findelements(by.cssselector("div._4-u2.mbm._5v3q._4-u8 div._5pbx.usercontent")); } private static void scrolltobottom(webdriver driver) { long longscrollheight = (long) ((javascriptexecutor) driver).executescript("return math.max(" + "document.body.scrollheight, document.documentelement.scrollheight," + "document.body.offsetheight, document.documentelement.offsetheight," + "document.body.clientheight, document.documentelement.clientheight);" ); ((javascriptexecutor) driver).executescript("window.scrollto(0, " + longscrollheight + ");"); } public static void waitforpageloaded(webdriver driver) { expectedcondition<boolean> expectation = new expectedcondition<boolean>() { @override public boolean apply(webdriver driver) { return ((javascriptexecutor) driver).executescript( "return document.readystate").equals("complete"); } }; webdriverwait wait = new webdriverwait(driver, 20); wait.until(expectation); }
Comments
Post a Comment