Vignesh Paramasivam Vignesh Paramasivam - 1 year ago 226
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.


method help? Or do i need to use
until i find the searched item?

Answer Source

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 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();
            //Clicking on an element inside grid to get it into focus
            WebElement ele=null;
            int flag=0;
            int count=0;
                    //element to search for while scrolling in grid
                    ele = driver.findElement(By.xpath("//*[@id='ContentHolder_ASPxGridView1_DXMainTable']//td[.='3/28/1996']"));
                } catch(Throwable e){
                    //scrolling the grid using the grid's xpath
            }while((flag==0) || ((++count)==250));
                System.out.println("Element has been found.!!");
                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.
        //For highlighting the element to be located after scroll
        public static void highlightElement(WebDriver driver, WebElement ele) {
                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.

  • Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download