Environment
Python3.6+selenium3.0.2+ie11+win7
"Positioning Method"
1. Location by ID
Method: find_element_by_id (' xx ')
2. Positioning by name
Method: Find_element_by_name (' xx ')
3. Positioning by class name
Method: Find_element_by_class_name (' xx ')
4. Positioning by tag name
Method: Find_element_by_tag_name (' xx ')
Description: Tag name is the name of the tag in HTML, and there will be a lot of labels on a page with the same name. This positioning method is useful when you need to locate a set of elements.
Example
Description: Tag name can not be successfully located, the other several can successfully locate Baidu search box.
ImportUnitTest fromSeleniumImportWebdriverImport Timeclasslearnelement (unittest. TestCase):#Initialize, open browser defsetUp (self): Self.driver=Webdriver. Firefox () Self.driver.get ('http://www.baidu.com') #--------element Positioning------------- #<input id= "kw" Name= "wd" class= "S_ipt" value= "" maxlength= "255" autocomplete= "off" > deftestgetelement (self):#Location by IDelement = self.driver.find_element_by_id ('kw') #navigate by class name #element=self.driver.find_element_by_class_name (' S_ipt ') #Navigate by name #element = self.driver.find_element_by_name (' wd ') #positioning by Teg name #element=self.driver.find_element_by_tag_name (' input ')Element.send_keys ('through ID') Submit= self.driver.find_element_by_id ('su') Submit.click () Time.sleep (5) #--------Close the browser------------ #def tearDown (self): #self.driver.quit ()if __name__=='__main__': Unittest.main ()
3 Python+selenium element positioning method (ID, class name, name, tag name)