Search code examples
javaseleniumxpathwebdriverwaitxpath-1.0

How to click the element from the table Search by specific text


How to click on element search by text within table. I have tried some code but its not working.

HTML:

<td _ngcontent-c8="" class="align-middle cursorPoint" tabindex="0">Shelton</td>

I want to click on this <tr> which having text Shelton in it.


Solution

  • The desired element is an Angular element so to locate and click() the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategy:

    • xpath 1:

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[@class='align-middle cursorPoint' and text()='Shelton']"))).click();
      
    • xpath 2:

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[text()='Shelton']"))).click();
      

    Update

    To achieve the same in a step-wise manner:

    WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[@class='align-middle cursorPoint' and text()='Shelton']")));
    elem.click();