Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to find element with css selector

Using Selenium Webdriver for FF/IE using C# (.Net)

Below is my page source and I am trying to use the CssSelector to find/contains the particular name from my page and i have tried with the below code but resulting in error, any help?

//code

driver.FindElement(By.CssSelector("td:contains('John John')"))

//error:

e {"Unable to find element with css selector == td:contains('John John')"}  System.Exception {OpenQA.Selenium.NoSuchElementException}

//my html code:

 <div id="ctl00_ContentPlaceHolder1_AddeCardControl1_gv_ctl01_RecordCount" style="float:right; padding-right:10px; margin-top:3px;">
  <b>308</b> Items Found
 </div>
 </td>
</tr>
<tr class="item">
 <td align="center"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$AddControl1$gv','Select$0')">Edit</a></td>
 <td align="center" style="width:15px;"></td>
 <td>John John</td>
 <td>&nbsp;</td>
 <td>&nbsp;</td>
 <td>&nbsp;</td>
 <td><img src="check.png" alt='Active' style='display: ;' /></td>
 <td>9/7/2012 11:15:08 PM</td>
</tr>
<tr class="altItem">
 <td align="center"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$AddControl1$gv','Select$1')">Edit</a></td>
 <td align="center" style="width:15px;"></td>
 <td>John Schulz</td>
 <td>&nbsp;</td>
 <td>Visitors</td>
 <td>&nbsp;</td>
 <td><img src="check.png" alt='Active' style='display: ;' /></td>
 <td>9/7/2012 6:28:29 PM</td>
</tr>
<tr class="item">
 <td align="center"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$AddControl1$gv','Select$2')">Edit</a></td>
 <td align="center" style="width:15px;"></td>
 <td>Parker Smith</td>
 <td>&nbsp;</td>
 <td>Visitors</td>
 <td>&nbsp;</td>
 <td><img src="check.png" alt='Active' style='display: ;' /></td>
 <td>9/7/2012 6:01:28 PM</td>
</tr>
<tr class="altItem">
 <td align="center"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$AddControl1$gv','Select$3')">Edit</a></td>
 <td align="center" style="width:15px;"></td>
 <td>Test 123</td>
 <td>&nbsp;</td>
 <td>Visitors</td>
 <td>&nbsp;</td>
 <td><img src="check.png" alt='Active' style='display: ;' /></td>
 <td>9/7/2012 1:36:45 PM</td>
</tr>
<tr class="item">
 <td align="center"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$AddControl1$gv','Select$4')">Edit</a></td>
 <td align="center" style="width:15px;">
like image 738
Nick Kahn Avatar asked Mar 11 '26 18:03

Nick Kahn


1 Answers

The :contains pseudoselector is not part of the W3C CSS Selector standard. As such, browsers do not support selecting elements using it. Some JavaScript CSS selector engines (Sizzle, the engine used by jQuery, for example) provide a :contains pseudoselector, but its presence cannot be relied on.

If you must find an element by the text contents of the element, your only solution at this point is to use XPath. A (very poorly performing) example of how to find this in your case would be as follows:

IWebElement element = driver.FindElement(By.XPath("//td[contains(., 'John John')"));

Note that a better solution will always be to have the application you're automating have proper IDs for the elements you need to find. You should be using text to find elements only as a last resort.

like image 131
JimEvans Avatar answered Mar 13 '26 11:03

JimEvans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!