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,320 points
15,512 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

How to check the state of a radio button or checkbox in Selenium?

Hey Priya, to check the state of ...READ MORE

answered May 7, 2019 in Selenium by Pratibha
• 3,690 points
4,592 views
0 votes
1 answer

How to automate radio button and checkbox on a webpage in Selenium Webdriver?

Hey Yashmita, following code snippet automates the ...READ MORE

answered Jul 23, 2019 in Selenium by Anvi
• 14,150 points
2,371 views
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 14, 2018 in Selenium by king_kenny
• 3,710 points
1,058 views
0 votes
2 answers

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,500 points
13,765 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,184 views
0 votes
2 answers

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

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

answered Sep 4, 2020 in Selenium by Sri
• 3,190 points
2,934 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,910 points
7,258 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,910 points
10,197 views
+1 vote
2 answers

Is it possible to scroll down in a webpage using selenium webdriver programmed on python?

I using next code for facebook for ...READ MORE

answered May 16, 2019 in Selenium by mslavikas@gmail.com
26,103 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP