ID element location
-
Id: Based on the value of the ID in the element attribute to locate, similar to the ID number on people’s ID card, will not be repeated without accident
driver.find_element_by_id('id') Copy the code
Name element positioning
-
Name: Locate based on the value of name in the element attribute, similar to the name on people’s ID cards, which may have duplicate names
driver.find_element_by_name('name') Copy the code
Link Text Element location
-
Link text: used to locate hyperlinks
driver.find_element_by_link_text('registered') Copy the code
Partial Link Text Element location
-
Partial Link text: Link text Fuzzy query version, similar to link % in the database. If the fuzzy query matches multiple elements that match the adjustment, select the first one
driver.find_element_by_partial_link_text("Book") Copy the code
Classname element location
-
Classname: Based on the element class style, it is very easy to encounter duplicates
driver.find_element_by_class_name("am-btn am-btn-secondary am-btn-xs am-radius") Copy the code
Tagname element positioning
-
Tagname: Indicates the name of a tag for locating. It has the highest repetition and is used only in the case of secondary filtering after locating
driver.find_elements_by_tag_name("a") Copy the code
Cssselector element location
-
Cssselector: Location by absolute path: rarely used this way
driver.find_element_by_css_selector("body > div.am-g.my-content > div > div.am-u-sm-12.am-u-md-6.am-u-lg-4.container-right > div.forgetpwd-top > a") Copy the code
Xpath element location
-
Xpath: 1. Location via absolute path, which is rarely used
driver.find_element_by_xpath("/html/body/div[4]/div/div[2]/div[1]/a") Copy the code
-
Xpath: 2. Locate based on the relative path in the format of //+ unique parent tag +/ child tag……
driver.find_element_by_xpath('//form/span/input') Copy the code
-
Xpath: 3. Locate by element attributes
# Single attribute positioning driver.find_element_by_xpath("//input[@name='pwd']") # Multi-attribute positioning driver.find_element_by_xpath('//a[@title="Tutorialspoint" and @rel="nofollow"]') Copy the code
-
Xpath: 4. Locate by element partial attributes
# start with a partial attribute value driver.find_element_by_xpath("//a[starts-with(@title,'Tutorial')]") # contains partial attribute values driver.find_element_by_xpath("//a[contains(@title,'Tutorial')]") driver.find_element_by_xpath("/ / a [contains (text (), 'baidu')]") Copy the code
-
Xpath: 5. Location by text
driver.find_element_by_xpath("//span[text()='Login']") Copy the code
For the efficiency of these lookup elements, id>name>xpath. Copy Xpath is not recommended because it is inaccurate in web pages. Some elements, whose attribute values change each time the page is refreshed, are called dynamic elements and can only be located in other ways.