你好,我是selenium的新手,我有一个关于如何使用selenium定位特定元素的问题。
我的HTML如下所示:
<div id="locator">
<table class = "table-1">
</table>
<div class="something else">
</div>
<table class = "table-1">
<tbody>
<tr>
<th>...</th>
<td>
<div class="adder">....</div>
</td>
</tr>
</table>
</div>问题是,我想在第二个表元素中找到div元素,有人能想出怎么做吗?提前谢谢。
发布于 2014-03-04 13:58:43
使用伪类:nth-of-type
driver.FindElement(By.CssSelector("table.table-1:nth-of-type(2) > tbody > tr > td > div.adder"));如果使用Selenium IDE,则使用
css=table.table-1:nth-of-type(2) > tbody > tr > td > div.adder见Selenium IDE文档。
当然,您也可以使用XPath而不是css (Ide.jsp#按xpath定位)。
css选择器定位器支持除名称空间以外的所有css1、css2和css3选择器,一些伪类(:nth-of-type,:nth-末段类型,:first-of-type,:first type,:only-of-type,:supports,:focus,:active,:focus,:namespace )和伪元素(::first-line,:first-信函,:selection,::前面,::后)
发布于 2014-03-04 14:15:13
试试这个..。
driver.FindElement(By.CssSelector("div#locator表:nth of type(2)div.adder“);
发布于 2014-10-22 16:43:44
我知道这是旧的,但是上面提到的选择器是错误的。CSS不支持某些伪类和伪元素。下面列出了不受支持的项目。
css=table.table-1:nth-child(2) > tbody > tr > td > div.adder
6.7。CSS CSS定位器策略使用CSS选择器查找页面中的元素。Selenium支持CSS 1到3个选择器语法例外的CSS3名称空间,以及以下内容:
pseudo-classes pseudo-elements
:nth-of-type ::first-line
:nth-last-of-type ::first-letter
:first-of-type ::selection
:last-of-type ::before
:only-of-type ::after
:visited
:hover
:active
:focus
:indeterminatehttps://stackoverflow.com/questions/22173607
复制相似问题