How to handle div that is created dynamically in a

2019-06-03 05:52发布

问题:

I am trying to iterate a table to get the results. The structure of the table looks as mentioned in the picture

When I open the page, there is only one with role="presentation" and I am able to retrieve the data using the below css locator.

driver.findElement(By.cssSelector("div[id^=dojox_grid__View_] div.dojoxGridContent div.dojoxGridRow:nth-child(1) tbody tr:nth-child(1) td:nth-child(6)")).getText();

When I scroll the page manually another tag is created dynamically with role="presentation" and has many rows(div.dojoxGridRow). I want to iterate these rows too.

Selenium is able to go only to first level (first ) and get the details. I am not sure how to reach the second level

回答1:

I'm not sure about which element you're trying to access.
But you can access to all div with 'presentation' role (return a list):

driver.findElements(By.cssSelector("div[role='presentation']"));  

If you're trying to access to each row under div with role presentation:

driver.findElements(By.cssSelector(".dojoxGridRow"));  

If you want to get rows child of div with role 'presentation':

List<WebElement> presentations = driver.findElements(By.cssSelector("div[role='presentation']"));
for (WebElement presentation : presentations) {
    List<WebElement> rows = presentation.findElements(By.cssSelector(".dojoxGridRow"));
    // DO SOMETHING...
}

Hope that helps.