无法使用css选择器查找元素 [英] Unable to find element with css selector
本文介绍了无法使用css选择器查找元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Selenium Webdriver for FF / IE使用C#(.Net)
Using Selenium Webdriver for FF/IE using C# (.Net)
下面是我的页面源代码,我试图使用CssSelector来查找/
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?
//代码
driver.FindElement(By.CssSelector("td:contains('John John')"))
//错误:
e {"Unable to find element with css selector == td:contains('John John')"} System.Exception {OpenQA.Selenium.NoSuchElementException}
//我的html代码:
//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;">
推荐答案
您可以尝试此
var webElements = (Driver.FindElements(By.XPath(elementXpath))).ToList();
webElements.FindIndex(item => item.Text.Contains("John John").Click()
b $ b
其中elementXpath是表names中每个单元格的路径,所以你得到名字列表,然后找到一个匹配,你会得到你的项目点击。
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.
这篇关于无法使用css选择器查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文