如何在表格中单击按文本搜索元素。我尝试了一些代码,但无法正常工作。

HTML:

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


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

最佳答案

所需的元素是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();

10-06 15:00