Vignesh Paramasivam Vignesh Paramasivam - 28 days ago 7
Java Question

How to search for element in dynamic loading grid on scroll using selenium webdriver?

There is Grid which has say 1000 rows with a column named Username(with distinct values).

And the grid will display only 20 rows per view, and the other rows will be loaded(ajax) only on scrolling.

So, how to search for a particular username in the grid, since we have only elements getting loaded on scroll.

Does

Scrollintoview
method help? Or do i need to use
window.scrollby()
until i find the searched item?

Answer

First of all, I apologise because I had never worked on a grid before. I thought it will be a frame and will be easier to switch and then scroll to the element using JavascriptExecutor. But, alas! That's not the case for a grid.
And, there must be a table when a grid is involved.

Now, this is what has worked for me.

  • First click on any visible element on grid to get it into focus.
  • Then scroll the grid using grid's locator(xpath,id,etc.) using 'Keys.PAGE_DOWN' till you find the element you are looking for.
  • In case the element is not found on each scrolls, than handle the exception it raises and scroll again.

    Note: Do not forget to give some sleep time after each scroll.

    I have automated one sample grid, and have attached the sample working code below. Hope this helps in figuring out problem:

    import java.io.IOException;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class ScrollGrid{
    
        public static void main(String[] args) throws IOException, InterruptedException{
    
    
            WebDriver driver = new FirefoxDriver();
            driver.get("https://demos.devexpress.com/ASPxGridViewDemos/PagingAndScrolling/VirtualPaging.aspx");
            driver.manage().window().maximize();
    
            //Clicking on an element inside grid to get it into focus
            driver.findElement(By.xpath("//*[@id='ContentHolder_ASPxGridView1_DXMainTable']//td[.='9/30/1994']")).click();
    
            WebElement ele=null;
            int flag=0;
            int count=0;
    
            do{
                try{
                    //element to search for while scrolling in grid
                    ele = driver.findElement(By.xpath("//*[@id='ContentHolder_ASPxGridView1_DXMainTable']//td[.='3/28/1996']"));
                    flag=1;
                } catch(Throwable e){
                    //scrolling the grid using the grid's xpath
                    driver.findElement(By.xpath("//*[@id='ContentHolder_ASPxGridView1']//div[2]")).sendKeys(Keys.PAGE_DOWN);
                    Thread.sleep(3000);
                }
            }while((flag==0) || ((++count)==250));
    
            if(flag==1){
                System.out.println("Element has been found.!!");
            }else{
                System.out.println("Element has not been found.!!");
            }
    
            highlightElement(driver, ele); //For highlighting the element
            Thread.sleep(5000L); //to check if the element scrolled to is highlighted.
            driver.close();
        }
    
        //For highlighting the element to be located after scroll
        public static void highlightElement(WebDriver driver, WebElement ele) {
            try
            {
                for (int i = 0; i < 3; i++) 
                {
                    JavascriptExecutor js = (JavascriptExecutor) driver;
                    js.executeScript("arguments[0].setAttribute('style', arguments[1]);",ele, "color: red; border: 2px solid red;");
                }
            }
            catch(Throwable t)
            {
                System.err.println("Error came : " +t.getMessage());
            }
        }
    
    }
    

    Note: This works correctly now. It will come out of the loop in case the element is found, or if not found after 250 scrolls. '250' is a relative number. You can change it to the number of scrolls you want to perform on the grid.