我需要按类及其包含的文本查找元素。我试过这样但没有任何效果,请帮忙(对我来说最好选择使用 css,但 xpath 也可以):
Driver.FindElement(By.XPath("//td[contains(@class,'TestClass') and .//text()='TestText']"))
Driver.FindElement(By.CssSelector("td.TestClass:contains('TestText')"))
元素:
<td class="TestClass"> TestText</td>
<td class="TestClass"> TestText1</td>
<td class="TestClass"> TestText2</td>...
请您参考如下方法:
您可以尝试使用以下 XPATH 表达式:
//td[@class='TestClass'][text()='TestText']使用 FindElements 而不是 FindElement。您可以找到所有 td 元素,然后循环遍历它们以查找您需要的文本。
