Objective
Most people use selenium to locate elements using XPath positioning, because XPath basically solves the need for positioning. CSS positioning is often ignored, in fact, CSS positioning also has its value, CSS positioning faster, more concise syntax.
This CSS positioning method, mainly compared to the previous XPath, basically XPath can be done, CSS can also be done. Two comparative studies are easier to understand.
First, CSS: property positioning
The 1.css can be directly anchored to the three general properties of the element's ID, class, and tag.
2. The following is the HTML code of the Baidu input box:
<input id= "kw" class= "S_ipt" type= "text" autocomplete= "Off" maxlength= "+" name= "wd"/>
The 3.CSS uses the # number to denote the id attribute, such as: #kw
4.css. Represents the class attribute, such as:. S_ipt
5.CSS directly with the label name, without any identifiers, such as: input
Second, CSS: other properties
1.css In addition to the label, class, ID, the three general properties can be located outside, but also through other properties to locate
2. The following are the formats for locating other properties
Third, CSS: tags
A 1.css page can be used to position elements by combining labels with attributes
Iv. CSS: Hierarchical relationships
1. In the previous XPath, we talked about hierarchical relationship positioning, where CSS can achieve the same effect
2. xpath://form[@id = ' form ']/span/input and
form[@class = ' FM ']/span/input can also be implemented with CSS
Five, CSS: index
1. As an example, follow the previous article
2.css can also be indexed option:nth-child (1) to locate the child elements, which is very different from the XPath notation, in fact, very good understanding, direct translation came to the first few children
Vi. CSS: Logical operations
1.css can also implement a logical operation, matching two properties, which is not the same as XPath, no need to write the and keyword
Vii. CSS: Fuzzy matching
1.css fuzzy matching contains (' xxx '), although the Internet with a variety of data display can be used, but the small part of the test in person, has been an error.
2. After the various Baidu found the answer: You can ' t does this with the CSS selectors, because there is the no such Thing As:contains () in CSS. It was a proposal the was abandoned years ago.
Unfortunately, this syntax has been abandoned, so there is no further research.
CSS syntax far more than the above mentioned, there are more powerful positioning strategies, interested can continue in-depth study. Officially, CSS is positioned faster and syntax is more concise, but XPath is more intuitive and better understood.
Selenium2+python Automated 9-CSS localization syntax