A: UnitTest frame with selenium tool css_selector positioning.

Source: Internet
Author: User

Did the automated test so long, has not combed the element to locate this piece of content, its importance is self-evident. Take advantage of the weekend and have time to comb it.

1, location by ID

Driver.find_element_by_css_selector ("#kw"). Clear ()                   
Driver.find_element_by_css_selector ("#kw"). Send_keys ("Selenium")

2, through class positioning

Driver.find_element_by_css_selector ("input.bg"). Click ()
When there is a space between the values of class, just add a dot to the middle of the space.
Such as:
Driver.find_element_by_css_selector ("Div.clearfix.oe_login_buttons"). Click ()


3, by attribute positioning
Driver.find_element_by_css_selector ("[placeholder= ' Please enter password ']"). Clear ()

4, through the level of element positioning
Driver.find_element_by_css_selector ("Div.clearfix.oe_login_buttons>button.btn.btn-primary"). Click ()


Often used in the work of the positioning method through the son to find a father, and then find the father's brother. I don't know if CSS can be implemented. Further additions and updates. Anyway, XPath can be implemented.
Finally, a complete code.
From selenium import Webdriver
Import time
Driver=webdriver. Chrome ()
Driver.maximize_window ()
Driver.implicitly_wait (20)
Driver.get ("http://www.***************")
Driver.find_element_by_css_selector ("[name= ' login ']"). Clear ()
Driver.find_element_by_css_selector ("[name= ' login ']"). Send_keys (U "Gorilla")
Driver.find_element_by_css_selector ("[placeholder= ' Please enter password ']"). Clear ()
Driver.find_element_by_css_selector ("[placeholder= ' Please enter password ']"). Send_keys ("13*********")
Driver.find_element_by_css_selector ("Div.clearfix.oe_login_buttons>button.btn.btn-primary"). Click ()
Time.sleep (3)
Driver.quit ()

A: UnitTest frame with selenium tool css_selector positioning.

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.