在selenium中使用css选择器进行元素定位
Sizzle Css3还提供一些直接选取form表单元素的伪类
:input
: Finds all input elements (includes textareas, selects, and buttons).:text
,:checkbox
,:file
,:password
,:submit
,:image
,:reset
,:button
: Finds the input element with the specified input type (:button also finds button elements).
下面是一些XPATH和CSS的同义locator比较
定位方式 |
XPath |
CSS |
标签 |
//div |
div |
By id |
//div[@id='eleid'] |
div#eleid |
By class |
//div[@class='eleclass'] |
div.eleclass |
By 属性 |
//div[@title='Move mouse here'] |
div[title=Move mouse here] |
定位子元素 |
//div[@id='eleid']/* |
div#eleid >* |
定位后代元素 |
//div[@id='eleid']//h1 |
div h1 |
By index |
//li[6] |
li:nth(5) |
By content |
//a[contains(.,'Issue 1164')] |
a:contains(Issue 1164) |
根据子元素回溯定位父元素 |
//li[a[contains(.,'Issue 1244')]] |
li{a:contains(Issue 1244)} |
根据邻近元素定位 |
//li[preceding-sibling::li[contains(.,'Issue 1244')]] |
css=li:contains(Issue 1244) + li |