Unable to grab an element in a dynamic table where we have only text of a td tag Unable to grab an element in a dynamic table where we have only text of a td tag selenium selenium

Unable to grab an element in a dynamic table where we have only text of a td tag


You are trying using -

el1.findElements(By.xpath("//following-sibling::td[4]/div[1]"));

It is matching all the element present with format td[4]/div[1] in your page and retrieving first match.

You have to use following xpath to grab status present under div based on you text.

driver.findElement(By.xpath(".//tr/td[contains(.,'text1')]/following-sibling::td[3]/div")).getAttribute("class");

If your requirement to extract all status try following code-

 List<WebElement> allElements = driver.findElements(By.xpath(".//tr/td[contains(.,'text2')]/following-sibling::td[3]/div")); for(WebElement element:allElements) {    String status = element.getAttribute("class");    System.out.println(status);}


I think this approach suitable for you:

Get all div elements containt attribute status :

List<WebElement> listChildStatus = driver.findElements(By.xpath(".//tr[.//a[contains(.,'text')]]//div"));

Get specific div elements containt attribute status :

WebElement childStatus = driver.findElement(By.xpath(".//tr[.//a[contains(.,'{TEXT}')]]//div"));

{TEXT} = the text value that you have


You need to locate the element with the text, go one level up, and then get the sibling with the status

WebElement statusElement = driver.findElement(By.xpath(".//td[a[contains(text(),'text2')]]/following-sibling::td[3]/div"));String status = statusElement.getAttribute("class"); // will be status2

If you don't want to relay on index you can use last() to find the last sibling

WebElement statusElement = driver.findElement(By.xpath(".//td[a[contains(text(),'text2')]]/following-sibling::td[last()]/div"));