Scrolling with phantomJs Selenium

2019-04-12 11:56发布

问题:

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?

回答1:

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


回答2:

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.