How to search for an element in a dynamic loading grid when scrolling with selenium webdriver?

There is a grid that says 1000 rows with a column named Username (with different values).

And the grid will only display 20 rows per view, and the rest of the lines will be loaded (ajax) only when scrolling.

So, how to search for a specific username in the grid, since we only have items loaded in the scroll.

Does the Scrollintoview method Scrollintoview ? Or do I need to use window.scrollby() until I find the item I am looking for?

+5
source share
2 answers

First of all, I apologize because I had never worked on a grid before. I thought it would be a frame, and it would be easier to switch and then scroll the item to a JavascriptExecutor . But alas! This does not apply to the grid.
And there should be a table when the grid is involved.

Now this is what worked for me.
First click on any visible element on the grid to get it in focus. Then scroll through the grid using the grid locator (xpath, id, etc.) using "Keys.PAGE_DOWN" until you find the item you need. If the element is not found on each scroll, than it handles the exception, which it raises and scrolls again.

Note. Remember to give some sleep time after each scroll.

I automated one grid with a sample and attached an example of working code below. Hope this helps in solving the 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 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: Now this works correctly. It will exit the loop if an element is found or if it is not found after 250 scrolls. '250' is a relative number. You can change it to the number of scrolls you want to perform in the grid.

+4
source

I would take "ScrollintoView" any day.
It will scroll and check the item at the same time and stop when it has the item in appearance

For help with the code, please follow this link.

0
source

Source: https://habr.com/ru/post/1206141/


All Articles