Scrolling with phantomJs Selenium

2019-04-12 12:11发布

I am trying to get this particular code running for scrolling a webpage which is a type of pagination. It works like a charm with the Firefox driver, but when i use phantomJS it doesn't work and goes into infinite loop

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

}

Here is the code which executes scrolling

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

}

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?

2条回答
Ridiculous、
2楼-- · 2019-04-12 12:15

LinkedIn is changing the page when you scroll to the bottom, requesting more data. That means you never get the same result after the scrolling.

I'm not sure why you don't see in Firefox; maybe it handles the scroll event after you call getPageSource() or getPageSource() returns stale data.

查看更多
forever°为你锁心
3楼-- · 2019-04-12 12:30

Got the answer, I called an explicit wait. and it works fine

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;
    }
查看更多
登录 后发表回答