Selenium Tips - 从XPATH到CSS

原文:
http://sauceio.com/index.php/2009/10/selenium-tip-of-the-week-start-improving-your-locators/

Almost everyone in the Selenium world knows of the power of XPATH as a locating strategy, at the same way, most of them also know of its slowness on IE.
Now, if your tests have relatively simple XPATH  locators, putting some effort to change them to CSS (which has native support on all browsers) is a very good investment and will impact your tests with instant time improvements.
For this week’s tip, we’ve decided to start addressing the basic rules for the move of simple locators from XPATH to CSS.

Direct child

A direct child in XPATH is defined by the use of a “/“, while on CSS, it’s defined using “>
Examples:

[backcolor=white !important][size=1em]

[size=1em][backcolor=white !important][size=1em]//div/a

[backcolor=white !important][size=1em]css=div > a



Child or subchild

If an element could be inside another or one it’s childs, it’s defined in XPATH using “//” and in CSS just by a whitespace
Examples:

[backcolor=white !important][size=1em]

[size=1em][backcolor=white !important][size=1em]//div//a

[backcolor=white !important][size=1em]css=div a



Id

An element’s id in XPATH is defined using: “[@id='example']” and in CSS using: “#
Examples:

[backcolor=white !important][size=1em]

[size=1em][backcolor=white !important][size=1em]//div[@id='example']//a

[backcolor=white !important][size=1em]css=div#example a



Class

For class, things are pretty similar in XPATH: “[@class='example']” while in CSS it’s just “.
Examples:

[backcolor=white !important][size=1em]

[size=1em][backcolor=white !important][size=1em]//div[@class='example']//a

[backcolor=white !important][size=1em]css=div.example a


Thats’ all for now, as you can see, the first rules a pretty simple, and you even make you locators shorter and cleaner.

posted on 2013-04-26 16:28  ibelieve  阅读(300)  评论(0编辑  收藏  举报

导航