How to select first instance of a button in a table row based on cell content by XPath

0 votes

I am not able to click on a button in the first cell if the fourth cell text = zero. I am trying to iterate through the table rows until I come across [td[1][button[text() = 'Review'] and td[4][a[text() = '0']

Below is the code:

IWebElement baseTable = Browser.Driver.FindElement(By.XPath("//*[@id='summaryGrid']/table/tbody"));

ICollection<IWebElement> rows = baseTable.FindElements(By.TagName("tr"));

foreach (var row in rows)

{

    bool x = row.FindElement(By.XPath("//tr[td[1][button[text() = 'Review']] and td[4][a[text() = '0']]]")).Displayed;


    if (x == true)

    {

        row.FindElement(By.XPath("//tr[td[1][button[text() = 'Review']] and td[4][a[text() = '0']]]//button[0]")).Click();

        break;

    }

}

Jun 6, 2018 in Selenium by Martin
• 4,220 points
3,050 views

1 answer to this question.

0 votes

This xpath should give you all the first buttons in the first cells by the conditions you gave

This will help you.

//tr/td[1][/button[text() = 'Review'] and /td[4]/a[text() = '0']]/button[1]

Iterate through it and click() on every element.

ICollection<IWebElement> buttons = baseTable.FindElements(By.XPath("//tr/td[1][/button[text() = 'Review'] and /td[4]/a[text() = '0']]/button[1]"));

foreach (var button in buttons)

{

    button.Click();

}

answered Jun 6, 2018 by jons

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to select an Object by its class in Selenium?

When it comes to Selenium, XPath will ...READ MORE

answered Apr 13, 2018 in Selenium by king_kenny
• 3,610 points
35 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,050 points
408 views
0 votes
1 answer
0 votes
1 answer

What is the role of TestNG & JUnit frameworks in Selenium?

Well TestNG and JUnit are test frameworks ...READ MORE

answered Apr 18, 2018 in Selenium by king_kenny
• 3,610 points
215 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
766 views
0 votes
1 answer

(Protractor) Finding specific row in ng-table by text

Try this Return the filtered elements before trying ...READ MORE

answered May 10, 2018 in Selenium by Samarpit
• 5,130 points
3,655 views
+1 vote
2 answers