各 ‘td’タグの中に入れ子にされた動的divの数を取得するにはどうすればよいですか?

I have table which have several <td> tags,
again these tag have nested <div> tag. But the
number of <div> are dynamic. Means, for each
<td> tag number of <div> are
different.

I wanted to find the total number of <div>
inside a <td> and iterate them to grab each
div’s value.

I’ve tried using below xpath, but I’m not sure how can I get the
count of <div>‘s for loop through them.

for(int i=1;i<=NoOfDivs;i++) 
{
    WebElement NestedDivs= driver.findElement(By.xpath("/html/body/table[4]/tbody/
    tr[3]/td[2]/div["+i+"]"));
}

Webpage HTML is given in the below snap:

enter image description here

ベストアンサー

Make sure you are locating the correct table. Based on
information provide by you. Please refer the below code snippet to
find all the <div> and iterate through.

List allElements = driver.findElements(By.xpath("//table[4]/tbody/tr[3]/td[2]/div"));
int totalDivs = allElements.size();
System.out.println("total divs :"+totalDivs)

for(WebElement element : allElements){
    System.out.println("Value :"+element.getText());
}

返信を残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です