如何单击 table 按特定文本搜索的元素

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

如何在 table 中通过文本点击元素搜索。我已经尝试了一些代码,但它不起作用。

HTML:

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

我想点击这个 <tr>,里面有文字 Shelton

所需的元素是 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 :

  • 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();
    

更新

要以 step-wise 方式实现相同的目的:

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