Selenium with Python 003-page element positioning

Source: Internet
Author: User
Tags element groups tag name selenium python

WebUI Automation, we first need to locate the elements to be manipulated in the page, and then perform various event operations, here we first describe how selenium Python locates page elements, Webdriver provides a series of methods.

Locates a single page element (returns a single Element object)

    • find_element_by_id
    • Find_element_by_name
    • Find_element_by_xpath
    • Find_element_by_link_text
    • Find_element_by_partial_link_text
    • Find_element_by_tag_name
    • Find_element_by_class_name
    • Find_element_by_css_selector

It is important to note that the above method, when matched to multiple objects, can only return the first element object anchored to, and throws an exception NoSuchElementException when no match is reached to any element object

Positioning page Element groups (returns a list of element objects)

    • Find_elements_by_name
    • Find_elements_by_xpath
    • Find_elements_by_link_text
    • Find_elements_by_partial_link_text
    • Find_elements_by_tag_name
    • Find_elements_by_class_name
    • Find_elements_by_css_selector

HTML Template Example1

<HTML> <Body>  <formID= "LoginForm">   <inputname= "username"type= "text" />   <inputname= "Password"type= "Password" />   <inputname= "Continue"type= "Submit"value= "Login" />   <inputname= "Continue"type= "button"value= "Clear" />  </form></Body><HTML>
Location by ID
Login_form = driver.find_element_by_id ('loginform')
Navigate by name
Username = driver.find_element_by_name ('username'= driver.find_element_by_ Name ('password')
Positioning via XPath
Login_form = Driver.find_element_by_xpath ("/html/body/form[1]"= Driver.find _element_by_xpath ("//form[1]"= Driver.find_element_by_xpath ("  //form[@id = ' loginform ']")
 username = Driver.find_element_by_xpath ( " / /form[input/@name = ' username ']   " ) Username  = Driver.find_element_by_xpath ( " //form[@id = ' loginform ']/input[1]   " ) Username  = Driver.find_element_by_xpath ( //input[@name = ' username ']   ") 
Clear_button = Driver.find_element_by_xpath ("//input[@name = ' Continue '] [@type = ' button ']" = Driver.find_element_by_xpath ("//form[@id = ' loginform ']/input[4]") 

HTML Template Example2

<HTML> <Body>  <H1>Welcome</H1>  <Pclass= "Content">Is you sure your want to does this?</P>  <ahref= "continue.html" name= "tj_continue" title= "web" class= "RecycleBin xz">Continue</a>  <ahref= "cancel.html">Cancel</a></Body><HTML>
Positioning by linking text
Continue_link = Driver.find_element_by_link_text (' Continue ') Continue_link = Driver.find_element_by_partial_link_ Text (' Conti ')
Locating by tag name
Heading1 = Driver.find_element_by_tag_name ('H1')
Through class positioning
Content = Driver.find_element_by_class_name ('content')
Positioning via CSS Selector
Content = Driver.find_element_by_css_selector ('p.content')
Continue_link = Driver.find_element_by_css_selector ("a[name=\" tj_continue\ "]")
Continue_link = Driver.find_element_by_css_selector (' a[title= "web"] ")
Continue_link = Driver.find_element_by_css_selector (' A.recyclebin ')

Selenium with Python 003-page element positioning

Related Article

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.