Peter Penzov
Peter Penzov

Reputation: 1668

Click on table row if text is found

I use this Java code with Selenium to select table row based on found text:

WebElement tableContainer = driver.findElement(By.xpath("//div[@class='ag-center-cols-container']"));

        List<WebElement> list = tableContainer.findElements(By.xpath("./child::*"));

        // check for list elements and print all found elements
        if(!list.isEmpty())
        {
            for (WebElement element : list)
            {
                System.out.println("Found inner WebElement " + element.getText());
            }
        }

        // iterate sub-elements
        for ( WebElement element : list )
        {
            System.out.println("Searching for " + element.getText());

            if(element.getText().equals(valueToSelect))
            {
                element.click();
                break;  // We need to put break because the loop will continue and we will get exception
            }
        }

Full code: https://pastebin.com/ANMqY01y

For some reason table text is not clicked. I don't have exception. Any idea why it's not working properly?

Upvotes: 1

Views: 387

Answers (1)

cruisepandey
cruisepandey

Reputation: 29362

See there are 2 divs with //div[@class='ag-center-cols-container'] with this xpath.

first div does not have anything, while second div has child divs.

I would suggest you to use :

List<WebElement> list = driver.findElements(By.xpath("//div[@class='ag-center-cols-container']//div"));

Remove this line from your code :

WebElement tableContainer = driver.findElement(By.xpath("//div[@class='ag-center-cols-container']"));

Upvotes: 1

Related Questions