Unable to find element with css selector
Solution 1
You can try this
var webElements = (Driver.FindElements(By.XPath(elementXpath))).ToList();
webElements.FindIndex(item => item.Text.Contains("John John").Click()
where "elementXpath" is path to each cell in table "names". So you get the list of names and then just find a match. You'll get your item clicked.
Solution 2
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.
Comments
-
Nick Kahn almost 2 years
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> </td> <td> </td> <td> </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> </td> <td>Visitors</td> <td> </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> </td> <td>Visitors</td> <td> </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> </td> <td>Visitors</td> <td> </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;">
-
Nick Kahn over 11 yearsJim, I hear you, what is the best way to approach for that?
-
JimEvans over 11 yearsThis is one of the few cases where you have to rely on XPath as your selector. Otherwise, fix the site to provide proper IDs for the <td> elements you want to locate.
-
Nick Kahn over 11 yearswhat is webElements ? is it IWebElements? I tried to use
IWebElements
but throwing me an error... Error'OpenQA.Selenium.IWebElement' does not contain a definition for 'FindIndex' and no extension method 'FindIndex' accepting a first argument of type 'OpenQA.Selenium.IWebElement' could be found (are you missing a using directive or an assembly reference?)
-
Vlad Titov over 11 yearsSorry, it's my fault, surely, it is a variable. I corrected post.
-
Nick Kahn over 11 yearstried your latest code, got this error message
The xpath expression '//td[contains(., 'John John')' cannot be evaluated or does notresult in a WebElement
-
JimEvans over 11 yearsThis will be incredibly inefficient, compared to letting the XPath engine select the correct element for you. The XPath "contains()" function is the proper way to accomplish this.
-
Nick Kahn about 11 yearsie9 64 bit emulator? what do you mean by that
-
obesechicken13 about 11 yearsSorry. The IE9 64bit driver. I am using the IEdriver that runs on 64bits.