Selenium Getting Started: CSS positioning

Source: Internet
Author: User
Tags xpath

Selenium strongly recommends using CSS positioning instead of XPath to locate elements, because CSS positioning is faster than XPath positioning and syntax is more concise.

CSS Common positioning method

1.find_element_by_css_selector ()

2. #id the ID selector to locate the element based on the id attribute

3.. class class selector, locating elements according to class attribute values

4. [attribute= ' value '] locates elements based on attributes

5.element>element to locate parent > child elements based on element level

Example:

From selenium import Webdriver

From time import sleep

Driver=webdriver. Firefox ()

Driver.get ("http://www.baidu.com")

#根据id定位

Driver.find_element_by_css_selector ("#kw"). Send_keys ("LoadRunner")

#根据class定位

Driver.find_element_by_css_selector (". S_ipt"). Send_keys ("VB")

#根据属性定位

Driver.find_element_by_css_selector ("[autocomplete= ' off ']"). Send_keys ("Selenium")

Sleep (3)

driver.find_element_by_id ("su"). Click ()

Sleep (5)

Driver.quit ()

Driver.get ("Http://www.***.net")

Sleep (2)

#根据层级定位

Driver.find_element_by_css_selector ("Form#loginform>u1>input"). Send_keys ("User name")

Driver.find_element_by_css_selector ("form#loginform>u1>input[name= ' password ')"). Send_keys ("password")

Selenium Getting Started: CSS 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.