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