3 Python+selenium element positioning method (ID, class name, name, tag name)

Source: Internet
Author: User
Tags tag name

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)

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.