Unable to find element with css selector

匿名 (未验证) 提交于 2019-12-03 01:39:01

问题:

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;"> 

回答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.



回答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.



回答3:

You may have better luck using the javascript executor to click the element. I am using a very slow IE9 64bit emulator and it seems the only way to click on certain buttons is to use the javascript executor.



回答4:

CSS selectors aren't very useful here, because CSS selectors work on the html structure i.e. type, relationship and attributes of web elements; they don't work well on the html content, which in this case is the internal text content 'John John'.

But, xpath will work for this job. The function you need is text() which returns the element's inner text content:

//td[text()='John John'] 

So your webdriver code should look like this:

driver.FindElement(By.xpath("//td[text()='John John']")); 

P.S. All locators verified using Firepath in firefox.



回答5:

You can use the below code:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!