Web Automation Testing---CSS way to position page elements

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.