Monday, January 6, 2014

Scroll Page Using Java Script And Find Dynamically Loaded Element


Presently we can see that total webelement is not loaded at one moment rather element loaded when we scroll the pages.

So here we choose "Jabong" for an example page.Here more product list generated when we go down slowly .We stop scrolling when we find our desired product and click on this product.

To Scroll the page we need to fire the java script. For this we follow this steps:

Step1:
import org.openqa.selenium.JavascriptExecutor;

Step2:
((JavascriptExecutor) driver).executeScript("window.scrollBy(0,100)", "");


Now problem is how to find element which we want, because element loaded when page scrolled.
We get exception if element is not present on the page so we should catch this exception and scroll the page again to find the element until the page end.



Sample Code is given below:

import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 *
 * @author Administrator
 */
public class Scroll {

    public static void main(String args[]) {

        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.navigate().to("http://www.jabong.com/men/clothing/"
                + "?source=topnav");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        System.out.println("Close the modal popup");
        driver.findElement(By.id("jab-vchr-cls")).click();
 /**
 * while(
!reachedbottom) loop is required to search the
 * element until page end .We put find
 * element within try-catch and if it get
 * exception it scroll the page and again
 * try to find the element.
 */


        boolean reachedbottom = Boolean.parseBoolean(js.executeScript("return $(document)"
                + ".height() == ($(window).height() + $(window).scrollTop());").toString());
        while (!reachedbottom) {
            ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,600)", "");
            try {
                reachedbottom = Boolean.parseBoolean(js.executeScript("return $(document).height() "
                        + "== ($(window).height() + $(window).scrollTop());").toString());
                WebElement element = driver.findElement(By.xpath("//*[@id='http://static3.jassets.com/p/The-Indian-Garage-Co.-Checks-Red-Casual-Shirt-2889-679124-1-catalog.jpg']/img"));
                Wait<WebDriver> wait_element = new WebDriverWait(driver, 10);
                wait_element.until(ExpectedConditions.elementToBeClickable(element));
                element.click();
                System.out.println("!!!!!!!!!!!!!!At Last Get Success!!!!!!!!!!!!!!!!");
                break;
            } catch (Exception ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println(ex.getMessage());
            }
        }
    }
}






No comments:

Post a Comment