"Selenium 3+java Automation (6)"-css positioning syntax

Source: Internet
Author: User

First, preface

This section explains CSS positioning.

The advantage of CSS positioning with respect to XPath positioning is that CSS is positioned faster and syntax is more concise.

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.

Second, CSS: attribute positioning

1, through the id attribute positioning, need to add the identifier "#", such as: #kw;

        element = Driver.findelement (By.cssselector ("#kw"));        Element.sendkeys ("Selenium");    

2, by the Class property positioning, you need to add an identifier ".", such as:. S_ipt;

        element = Driver.findelement (By.cssselector (". S_ipt"));        Element.sendkeys ("Selenium");

3, through the label attribute positioning, do not need any identifiers, such as: input;

        // The operation here will be an error, because the label "input" is not unique;    element = Driver.findelement (By.cssselector ("input"));    Element.sendkeys ("Selenium");

Third, CSS: Positioning through 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 attributes;

1) CSS locates elements through the name attribute;

        element = Driver.findelement (By.cssselector ("#su"));

2) CSS: Positioning elements through the AutoComplete property;

        element = Driver.findelement (By.cssselector ("[autocomplete = ' off ']");            Element.sendkeys ("AutoComplete");

3) CSS locates elements through the type attribute;

element = Driver.findelement (By.cssselector ("[type = ' Submit ']");           Element.click ();

Iv. CSS: Tags

1. CSS page can be used to locate elements by combination of tags and attributes;

1) CSS positioning elements through the combination of tag and ID attributes;

      element =driver.findelement (By.cssselector ("input#kw"));           Element.sendkeys ("Selenium");

2) CSS positioning elements through tags and class attributes;

       element = Driver.findelement (By.cssselector ("Input.s_ipt"));         Element.sendkeys ("Selenium");

3) CSS positioning elements through the combination of tags and other attributes;

element = Driver.findelement (By.cssselector ("Input[autocomplete = ' off ')");        Element.sendkeys ("Selenium");

Five, CSS: hierarchical relationship positioning

        ID property of form
element = Driver.findelement (By.cssselector ("Form#form>span>input")); Element.sendkeys ("444");
the class attribute of the//form
= Driver.findelement (By.cssselector ("Form.fm>span>input")); Element.sendkeys ("444");

Vi. CSS: Indexing

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 be the first few children;

element = Driver.findelement (By.cssselector ("Select#ft>option:nth-child (1)"); Element.click ();

element = Driver.findelement (By.cssselector ("Select#ft>option:nth-child (2)"); Element.click ();
element = Driver.findelement (By.cssselector ("Select#ft>option:nth-child (3)");
Element.click ();

CSS positioning far more than the above several methods, there are more powerful positioning strategies, interested students can continue to further research.

"Selenium 3+java Automation (6)"-css positioning syntax

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.