Webdriver in Python3.x:selenium for page element positioning

Source: Internet
Author: User

Webdriver in Python3.x:selenium for page element positioning

The elements on the page are like people, with various attributes, such as element name, element ID, element attribute (class attribute, name attribute), and so on. Webdriver are positioned using these attributes of the element.

Common element properties that can be used for positioning:

    • Id
    • Name
    • Class name
    • Tag name
    • Link text
    • Partial link text
    • Xpath
    • CSS Selector

The methods corresponding to locating an element in Webdriver are:

    • Driver.find_element_by_name ()
    • DRIVER.FIND_ELEMENT_BY_ID ()
    • Driver.find_element_by_class_name ()
    • Driver.find_element_by_tag_name ()
    • Driver.find_element_by_link_text ()
    • Driver.find_element_by_partial_link_text ()
    • Driver.find_element_by_xpath ()
    • Driver.find_element_by_css_selector ()

The method of locating a set of elements in the corresponding Webdriver:

    • Driver.find_elements_by_name ()
    • DRIVER.FIND_ELEMENTS_BY_ID ()
    • Driver.find_elements_by_class_name ()
    • Driver.find_elements_by_tag_name ()
    • Driver.find_elements_by_link_text ()
    • Driver.find_elements_by_partial_link_text ()
    • Driver.find_elements_by_xpath ()
    • Driver.find_elements_by_css_selector ()

Examples of Use:

    • ID and name Locator
      #driver.find_element_by_id ('kw')  #Name driver.find_element_by_name ('tj_trnews')  
    • Class name and tag name positioning
      Driver.find_element_by_class_name ("s_ipt")  
    • link text and partial link text positioning
       #   find_element_by_link_text ( "   news   "  ) Find_element_by_link_text (  "  bar paste  Span style= "COLOR: #800000" > " )  #  Span style= "COLOR: #008000" by Partail link text to position  find_element_by_link_text (  new   " ) Find_element_by_link_text (  "  paste  Span style= "COLOR: #800000" "" 
  • XPath Positioning
    " "XPath is a document localization language. Because HTML can be seen as an implementation of XML, selenium users can use this powerful language to locate in Web applications. Absolute path method: from the root element, when the level of the element is very deep, the path will be long, the reading is not good, it is difficult to maintain. It is not recommended to make an absolute path such a method. Relative path method: Through Firebug very easy to get the relative path of the XPath, open the Firebug plugin, on the page search text box, you can display the XPath;" "#Search by XPath, any element with an id attribute of ' kw 'Driver.find_element_by_xpath ("//*[@id = ' kw ')"). Click ()#Search by XPath, any element that (* represents) the input property is ' kw 'Driver.find_element_by_xpath ("//input[@id = ' kw ')"). Click ()#the current element needs to be positioned according to the attributes of the parent directory#location by ID property of the previous level directoryFind_element_by_xpath ("//span[@id = ' Input-container ']/input") #location by id attribute on level three directoryFind_element_by_xpath ("//div[@id = ' HD ']/form/span/input") #positioned by the Name property on the three level directoryFind_element_by_xpath ("//div[@name = ' Q ']/form/span/input")
    • CSS Positioning
      # This positioning method is more difficult to understand

Webdriver in Python3.x:selenium for page element positioning

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.