如何在表格中单击按文本搜索元素。我尝试了一些代码,但是没有用。
HTML:
<td _ngcontent-c8="" class="align-middle cursorPoint" tabindex="0">Shelton</td>
我要单击其中包含文本 Shelton 的<tr>
。
答案 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();