CSS method is also a lot of methods of positioning, compared to the XPath more flexible, the following describes the use of the method (Baidu input box as an example)
1, through the tag to locate, can be written as follows:
Driver.find_element_by_css_selector (' input '). Click ()
This is to say that the tag input must be the only way to write, otherwise it can be positioned in other ways
2, through the ID to locate, can be written as follows:
Driver.find_element_by_css_selector (' #kw '). Click ()
ID preceded by a # can, very simple, also can and tag combination to write:
Driver.find_element_by_css_selector (' input#kw '). Click ()
3, through the class to locate, can be written as follows:
Driver.find_element_by_css_selector ('. S_ipt '). Click ()
Also very simple, of course, can also be combined with the tag to write:
Driver.find_element_by_css_selector (' Tag.s_ipt '). Click ()
4, through the parent-child relationship positioning, can be written as follows
Driver.find_element_by_css_selector (' Span>input '). Click ()
Here is the example through the tag, in fact, for the parent node and child nodes can be in accordance with the 1, 2, the other write, in the middle with > to connect, so that the uniqueness of the element can be guaranteed, such as:
Driver.find_element_by_css_selector (' span>input#kw '). Click ()
5, you can also use the tag in addition to the ID, other than class attributes to locate elements, such as the input box through the Name property to locate:
Driver.find_element_by_css_selector (' Input[name= "WD"]). Click ()
Attributes can be either with or without a value, and are written directly as follows:
Driver.find_element_by_css_selector (' input[name] '). Click ()
6, in addition to the form shown in 5, you can also filter the value of the property, as follows:
The value of the Name property starts with W
Driver.find_element_by_css_selector (' input[name^= "w"] '). Click ()
The value of the Name property ends with D
Driver.find_element_by_css_selector (' input[name$= "D"] '). Click ()
The value of the Name property contains the W character
Driver.find_element_by_css_selector (' input[name*= "w"] '). Click ()
Basically, with the CSS above, you can uniquely identify an element and, of course, a special case. This requires concrete treatment of specific problems.
Web Automation Testing---CSS way to position page elements