Search code examples
c#.netseleniumselenium-webdriverxpath

How to locate a text in a table and referring the element click on another element using Selenium in C#?


I am having trouble working my head out, as what is the best way to search a specific Text (CustId) on a web table & once I have found the search text then click on a Select-Button present at the end of the row. Now, I do have an ElementId for the table and progressive ElementId on each Select-Button Sample of the source code given below --

<table class="w-table w-table-zebra w-table-hover" id="cust-found-table">
        <tbody><tr>
          <th class="w-table-header w-table-width-60">Cust name</th>
          <th class="w-table-header w-table-width-25">Cust Id</th>
          <th class="w-table-header w-table-width-15"></th>
        </tr>
        <!----><tr>
          <td class="w-table-row">Superman</td>
          <td class="w-table-row">12345</td>
          <td class="w-table-row text-center">
            <button class="btn btn-sm w-btn-jetson px-4" type="button" id="cust-found-btn-0">Select</button>
          </td>
        </tr><tr>
          <td class="w-table-row">Spiderman</td>
          <td class="w-table-row">23456</td>
          <td class="w-table-row text-center">
            <button class="btn btn-sm w-btn-jetson px-4" type="button" id="cust-found-btn-1">Select</button>
          </td>
        </tr><tr>
          <td class="w-table-row">Batman</td>
          <td class="w-table-row">34567</td>
          <td class="w-table-row text-center">
            <button class="btn btn-sm w-btn-jetson px-4" type="button" id="cust-found-btn-2">Select</button>
          </td>
        </tr><tr>

This potentially may be a duplicate post, but couldn't find a similar one. Any help is appreciate.

Go get started I have done as below but couldn't get this working --

var theSelector = "button[id*='" + cust-found-btn + "']";    
IWebElement tableElement = driver.FindElement(By.Id("cust-found-table"));
        IList<IWebElement> tableRow = tableElement.FindElements(By.TagName("tr"));
        IList<IWebElement> rowTD;
        foreach (IWebElement row in tableRow)
        {
            rowTD = row.FindElements(By.TagName("td"));

            if (row.Text.Equals("searchtext"))
            {
                driver.FindElement(By.Id("cust-found-btn")).Click();
            }
        }

Solution

  • To invoke click() on the respective button with text as Select with reference to any of the Cust Id, you can write a function which will accept the Cust Id as a reference and invoke click() on the corresponding element with text as Select as follows:

    • Function:

      public void clickSelect(string CustID)
      {
          driver.FindElement(By.XPath("//table[@class='w-table w-table-zebra w-table-hover' and @id='cust-found-table']//tr//td[@class='w-table-row'][text()='" + CustID + "']//following::td[1]/button[@class='btn btn-sm w-btn-jetson px-4' and starts-with(@id,'cust-found-btn-')]")).Click();
      }
      
    • Now you can call the function with the desired Cust Id from anywhere within your script as follows:

      clickSelect("12345")
      //or
      clickSelect("23456")
      //or
      clickSelect("34567")