I need to find elements by class and text it contains. I tried like this but nothing worked, please help(for me it is better to select using css, but xpath is ok too):
Driver.FindElement(By.XPath("//td[contains(@class,'TestClass') and .//text()='TestText']"))
Driver.FindElement(By.CssSelector("td.TestClass:contains('TestText')"))
elements:
<td class="TestClass"> TestText</td>
<td class="TestClass"> TestText1</td>
<td class="TestClass"> TestText2</td>...
You can try using the following XPATH expression:
//td[@class='TestClass'][text()='TestText']
Use FindElements instead of FindElement. You can find all td elements and then just loop through them looking for the text you need.
Use below xpath:-
//td[text()=' TestText']
OR
//td[@class='TestClass' and text()=' TestText']
Hope it will help you :)
A different approach by using a CSS Selector and LINQ in C#:
var elements = WebDriver.FindElements(By.CssSelector("td.TestClass'"]));
var theElement = elements.FirstOrDefault( e => e.Text.Contains("TestText1"));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.