本文介绍了使用phantomJs Selenium滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试运行此特定代码以滚动网页,这是一种分页。它与Firefox驱动程序的魅力相似,但是当我使用phantomJS时它不起作用并进入无限循环
public class Drivers {
public WebDriver phJS()
{
File phantomjs = Phanbedder.unpack(); //Phanbedder to the rescue!
String[] phantomArgs = new String[] {
"--webdriver-loglevel=NONE"
};
DesiredCapabilities dcaps = new DesiredCapabilities();
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs.getAbsolutePath());
dcaps.setCapability( "phantomjs.cli.args", phantomArgs);
WebDriver driver = new PhantomJSDriver(dcaps);
phantomjs.delete();
return driver;
}
public static void main(String args[]) throws IOException
{
WebDriver wd=new FirefoxDriver();// Does Not work with new Drivers().phJS()
wd.get("http://www.snapdeal.com/products/mobiles-mobile-phones/filters/Form_s~Smartphones#plrty|Brand:HTC|Ram_s:1%20GB^ 2%20GB^ 3%20GB^ 512%20MB%20and%20Below|Form_s:Smartphones|");
wd= new PageScroll().scrollToBottom(wd);
List<WebElement> wele = wd.findElements(By.xpath("//*[@class=' product-image ']/a"));
for(WebElement we:wele)
{
System.out.println(we.getAttribute("href"));
}
wd.quit();
}
}
以下是执行滚动的代码
public class PageScroll {
WebDriver driver;
public WebDriver scrollToBottom(WebDriver driver) {
String oldpage="";
String newpage="";
do{
oldpage=driver.getPageSource();
((JavascriptExecutor) driver)
.executeScript("window.scrollTo(0, document.body.scrollHeight)");
newpage=driver.getPageSource();
System.out.println(oldpage.equals(newpage));
}while(!oldpage.equals(newpage));
return driver;
}
}
当我使用PhantomJS时它会进入无限循环,而我不明白为什么。是因为ajax脚本没有被执行?但如果是这样它应该走出循环,如果它滚动为什么它不像firefox驱动程序那样停止?
When i use PhantomJS it goes into infinite loop of do while, i do not understand why. Is it because the ajax script is not executed? but if so it should go out of the loop, and if its scrolling why doesnt it stops like firefox driver?
推荐答案
得到答案,我叫了一个明确的等待。它工作正常
public synchronized WebDriver scrollToBottom(WebDriver driver, WebElement element,int time) throws InterruptedException {
String oldpage="";
String newpage="";
do{
oldpage=driver.getPageSource();
((JavascriptExecutor) driver)
.executeScript("window.scrollTo(0, (document.body.scrollHeight))");
this.wait(time);
newpage=driver.getPageSource();
}while(!oldpage.equals(newpage));
return driver;
}
这篇关于使用phantomJs Selenium滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!