日期:2014-05-17  浏览次数:20980 次

selenium 进阶1: CSS SELECTOR

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

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

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

根据这个文章,

http://marakana.com/bookshelf/selenium_tutorial/locators.html

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

: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


:indeterminate


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

PROS :

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

CONS :

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