selenium 进阶1: CSS SELECTOR

发布时间: 2012-04-12 10:08:00

selenium 不如JQUERY对CSS支持的那么好一直是我心中的痛。

比如:找到页面中,找到一组 <a>, 然后再找到[2].parent()这个dom, jQuery得心应手,selenium 之前则只能借助于晦涩难懂的 xpath.  >_<

而 selenium 大牛们都是推荐 CSS selector的。所以。。。我找了一下解决方案,发现selenium 虽然使用了sizzle ( ,但是对某些selector还是不支持的,下面就是“不支持”的列表:


6.7. CSS

The CSS locator strategy uses CSS selectors to find the elements in the page. Selenium supports CSS 1 through 3 selectors syntax excepted CSS3 namespaces and the following:

pseudo-classes pseudo-elements
















  • css=div[id="pancakes"] > button[value="Blueberry"] selects the button with its value property set at Blueberry if children of the pancakes div


  • Much faster than XPath
  • Widely used
  • Provides a good balance between structure and attributes
  • Allows for selection of elements by their surrounding context


  • They tend to be more complex and require a steeper learning curve