Selenium Issue When Having tabular data in the form of DIV on a website where inside a DIV I have one div for every row

Tags: , , , ,



While writing Selenium automation test for a website having number of rows in the form of DIV inside one DIV. every row denotes one DIV. suppose dynamically if I have 5 rows then following code structure is there.

<div id="mainDiv">
<div id-"div1"><table>......</table></div>
<div id-"div2"><table>......</table></div>
<div id-"div3"><table>......</table></div>
<div id-"div4"><table>......</table></div>
<div id-"div5"><table>......</table></div>
</div>

I am fetching each row div/table/tr/td using XPath in for loop in my code and clicking on each. So that I can download pdf. But it is working fine for 19 DIV. for 20th DIV I am not able to find that in my page using XPATH. I am getting no such element exception. though I applied wait then I get that explicit condition failed issue. Can anyone having idea that its a scroll issue or anything else because of which I am not able to fine 20th and further more divs.

Answer

Probably it looks like that element is not in Selenium viewPort.

You have multiple ways to deal with this :

1. Scrolling :

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,250)");

This is not very optimal way. Now, You can try for each element scroll to view :

2. scrollIntoView :

Let’s say you have a list of web elements. (allDivs)

for (WebElement div : allDivs){
  ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", div);
// and the do click or whatever
}

3. Use of moveToElement

for(WebElement divs : allDivs) {
            new Actions(driver).moveToElement(divs).build().perform();
            // click on div here
        }


Source: stackoverflow