These usually take a locator
, which can be created by ID, XPATH Code, or Cascading Style Sheets (CSS)
. Getting the XPATH code can be as easy as selecting the element in developer tools or using something like Chropath
.
XPath简单,但不可靠。任何软件升级都有可能造成Path的变化。更不用说反扒技术的存在了。
ID最好,但不常有。
颠来倒去,还是CSS靠谱些。但学起来费劲。
不过,为了Selenium的目的,CSS还是有限费劲,可整。
A CSS Selector is a combination of an element selector and a value which identifies the web element within a web page. They are string representations of HTML tags, attributes, Id and Class. As such they are patterns that match against elements in a tree and are one of several technologies that can be used to select nodes in an XML document.
An element’s id in XPATH is defined using: [@id='example']
and in CSS using: #
- ID’s must be unique within the DOM.
XPath: //div[@id='exampleid']
CSS: #exampleid
The previous example showed //div
in the xpath. That is the element type, which could be input for a text box
or button
, img
for an image, or a
for a link.
Xpath: //input or
Css: =input
HTML pages are structured like XML, with children nested inside of parents. If you can locate, for example, the first link within a div, you can construct a string to reach it. A direct child in XPATH is defined by the use of a /
, while on CSS, it’s defined using >
.
XPath: //div/a
CSS: div > a
Writing nested divs can get tiring - and result in code that is brittle. Sometimes you expect the code to change, or want to skip layers. If an element could be inside another or one of its children, it’s defined in XPATH using //
and in CSS just by a whitespace.
XPath: //div//a
CSS: div a
For classes, things are pretty similar in XPATH: [@class='example']
while in CSS it’s just .
XPath: //div[@class='example']
CSS: .example
This is useful for navigating lists of elements, such as forms or ul items. The next sibling will tell selenium to find the next adjacent element on the page that’s inside the same parent. Let’s show an example using a form to select the field after username.
<form class = "form-signin" role = "form" action = "/index.php" method = "post">
<h4 class = "form-signin-heading"></h4>
<input type = "text" class = "form-control" id = "username" name = "username" placeholder = "username" required autofocus></br>
<input type = "password" class = "form-control" id = "password" name = "password" placeholder = "password" required>
<p>
<button class = "btn btn-lg btn-primary btn-block radius" type = "submit" name = "login">Login</button>
</form>
Let’s write an XPath and css selector that will choose the input field after “username”. This will select the “alias” input, or will select a different element if the form is reordered.
XPATH: //input[@id='username']/following-sibling:input[1]
CSS: #username + input
If you don’t care about the ordering of child elements, you can use an attribute selector in selenium to choose elements based on any attribute value. A good example would be choosing the ‘username’ element of the form above without adding a class.
We can easily select the username element without adding a class or an id to the element.
XPATH: //input[@name='username']
CSS: input[name='username']
We can even chain filters to be more specific with our selectors.
XPATH: //input[@name='login'and @type='submit']
CSS: input[name='login'][type='submit']
Here Selenium will act on the input field with name=“login” and type=“submit”
CSS selectors in Selenium allow us to navigate lists with more finesse than the above methods. If we have a ul and we want to select its fourth li element without regard to any other elements, we should use nth-child
or nth-of-type
. Nth-child
is a pseudo-class. In straight CSS, that allows you to override behavior of certain elements; we can also use it to select those elements.
<ul id = "recordlist">
<li>Cat</li>
<li>Dog</li>
<li>Car</li>
<li>Goat</li>
</ul>
If we want to select the fourth li element (Goat) in this list, we can use the nth-of-type, which will find the fourth li in the list. Notice the two colons, a recent change to how CSS identifies pseudo-classes.
CSS: #recordlist li::nth-of-type(4)
On the other hand, if we want to get the fourth element only if it is a li element, we can use a filtered nth-child which will select (Car) in this case.
CSS: #recordlist li::nth-child(4)
Note, if you don’t specify a child type for nth-child it will allow you to select the fourth child without regard to type. This may be useful in testing css layout in selenium.
CSS: #recordlist *::nth-child(4)
In XPATH this would be similar to using [4].
CSS in Selenium has an interesting feature of allowing partial string matches using ^=, $=, or *=. I’ll define them, then show an example of each:
^= Match a prefix
CSS: a[id^='id_prefix_']
A link with an “id” that starts with the text “id_prefix_”
$= Match a suffix
CSS: a[id$='_id_sufix']
A link with an “id” that ends with the text “_id_sufix”
*= Match a substring
CSS: a[id*='id_pattern']
A link with an “id” that contains the text “id_pattern”