如何从表中单击元素按特定文本搜索

时间:2019-05-29 07:46:45

标签: java selenium xpath webdriverwait xpath-1.0

如何在表格中单击按文本搜索元素。我尝试了一些代码,但是没有用。

HTML:

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

我要单击其中包含文本 Shelton <tr>

1 个答案:

答案 0 :(得分:0)

所需元素是Angular元素,以便定位和click()元素,您必须使 WebDriverWait 成为可点击元素并且您可以使用以下任一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();
    

更新

要逐步实现相同目标:

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