简体   繁体   中英

How can I find element by two attributes? c# webdriver

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>...
  1. You can try using the following XPATH expression:

     //td[@class='TestClass'][text()='TestText'] 
  2. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM