問題描述
我有一個網(wǎng)頁,當(dāng)您滾動到底部時,它會通過 ajax 加載更多結(jié)果.您可以在它完成之前對其進(jìn)行多次迭代.有點像 facebook 的做法.
I have a webpage where when you scroll to the bottom it then loads more results via ajax. You can do several iterations of this before it completes. Bit like what facebook does.
我正在嘗試編寫一個 selenium 腳本以一直到頁面末尾,直到它完成.
I am trying to write a selenium script to keep going to the end of the page until it completes.
類似這樣的一半完成它..我只是不知道如何確定頁面是否在底部 - 所以我可以將它放在某種循環(huán)中?
Something like this sort of half completes it.. I just don't know how to determine if page is at the bottom - so i can put it inside a loop of some sort?
我的嘗試
By selBy = By.tagName("body");
driver.findElement(selBy).sendKeys(Keys.PAGE_DOWN);
System.out.println("Sleeping... wleepy");
Thread.sleep(5000);
driver.findElement(selBy).sendKeys(Keys.PAGE_DOWN);
System.out.println("Sleeping... wleepy1");
Thread.sleep(3000);
//etc...
會像這樣嗎?
hasScroll() 不是真正的方法.我把它放在那里是為了展示我想要實現(xiàn)的目標(biāo)
Could Look like this?
hasScroll() isnt a real method. I put that there to demonstrate what im trying to achieve
while (driver.findElement(By.tagName("body")).hasScroll()) {
driver.findElement(selBy).sendKeys(Keys.PAGE_DOWN);
System.out.println("Sleeping... wleepy");
Thread.sleep(2000);
}
推薦答案
試試這個方法:
//Get total height
By selBy = By.tagName("body");
int initialHeight = driver.findElement(selBy).getSize().getHeight();
int currentHeight = 0;
while(initialHeight != currentHeight){
initialHeight = driver.findElement(selBy).getSize().getHeight();
//Scroll to bottom
((JavascriptExecutor) driver).executeScript("scroll(0," + initialHeight + ");");
System.out.println("Sleeping... wleepy");
Thread.sleep(2000);
currentHeight = driver.findElement(selBy).getSize().getHeight();
}
希望有幫助!
這篇關(guān)于Selenium - 完整的ajax加載自動滾動到頁面底部的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!