gpt4 book ai didi

java - 使用 phantomJs Selenium 滚动

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:59:23 30 4
gpt4 key购买 nike

我正在尝试运行此特定代码以滚动网页,这是一种分页。它与 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 时,它会进入 do while 的无限循环,我不明白为什么。是不是ajax脚本没有执行?但如果是这样,它应该跳出循环,如果它滚动,为什么它不像 firefox 驱动程序那样停止?

最佳答案

得到答案,我叫了显式等待。并且工作正常

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;
}

关于java - 使用 phantomJs Selenium 滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30457201/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com