Selenium-locating Elements

Source: Internet
Author: User
Tags tag name

There is vaious strategies to locate elements in a page. You can use the most appropriate one for your case. Selenium provides the following methods to locate elements in a page:

    • 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

To find multiple elements (these methods would return a list):

    • 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

Apart from the public methods given above, there is both private methods which might be useful with locators in page objec Ts. These is the other private methods: find_element and find_elements.

Example usage:

bydriver.  Find_element(by.  XPATH'//button[text () = "Some text"] ')driver.  Find_elements(by.  XPATH'//button ')              

These is the attributes available for by class:

"id""xpath""link text""partial link text""name""tag name""class name""CSS Selector "      
1.1. Locating by Id

This is the use of the Know ID attribute of an element. With this strategy, the first element with the IDattribute value matching the location would be returned. If no element has a matching ID attribute, a nosuchelementexception would be raised.

For instance, consider this page source:

The form element can be located as this:

Driver.  find_element_by_id(' loginform ')    
1.2. Locating by Name

Use the If you know the name attribute of an element. With this strategy, the first element with the name attribute value matching the location would be returned. If no element has a matching nameattribute, a nosuchelementexception would be raised.

For instance, consider this page source:

The username & password elements can is located like this:

Driver.  Find_element_by_name(' username ')driver.  Find_element_by_name(' password ')          

This would give the "Login" button as it occur before the "Clear" button:

Continue = Driver.find_element_by_name (' Continue ')
1.3. Locating by XPath

XPath is the language used for locating nodes in an XML document. As HTML can be a implementation of XML (XHTML), Selenium users can leverage this powerful language to target elements in Their web applications. XPath extends beyond (as well as supporting) the simple methods of locating by ID or name attributes, and opens up all SOR TS of new possibilities such as locating the third checkbox on the page.

One of the main reasons for using XPath was when you had a suitable ID or name attribute for the element you wish to Locate. You can use XPath to either locate the element in absolute terms (not advised), or relative to an element that does has a N ID or name attribute. XPath Locators can also is used to specify elements via attributes other than ID and name.

Absolute XPaths contain the location of any elements from the root (HTML) and as a result is likely to fail with only the Slightest adjustment to the application. By finding a nearby element with an ID or name attribute (ideally a parent element) you can locate your target element bas Ed on the relationship. This was much less likely to the change and can make your tests more robust.

For instance, consider this page source:

The form elements can located like this:

 login_form = driver. Find_element_by_xpath ( "/html/body/form[1]" ) = driver. Find_element_by_xpath ( "//form[1]" ) login_form Span class= "o" >= driver. Find_element_by_xpath ( "//form[@id = ' loginform ']" )    
    1. Absolute path (would break if the HTML is changed only slightly)
    2. First FORM element in the HTML
    3. The form element with attribute named ID and the value loginform

The username element can be located as this:

 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 ']" )   
    1. First FORM element with a input child element with attribute named name and the value username
    2. First input child element of the form element with attribute named ID and the value loginform
    3. First INPUT element with attribute named ' name ' and the value username

The "Clear" button element can be located as this:

Driver.  Find_element_by_xpath("//input[@name = ' Continue '] [@type = ' button ']")driver.  Find_element_by_xpath("//form[@id = ' loginform ']/input[4]")      
    1. Input with attribute named name and the value continue and attribute named type and the V Alue Button
    2. Fourth input child element of the form element with attribute named ID and value loginform

These examples cover some basics, but on order to learn more, the following references is recommended:

    • W3Schools XPath Tutorial
    • The recommendation XPath
    • XPath Tutorial-with Interactive Examples.

There is also a couple of very useful add-ons that can assist in discovering the XPath of an element:

    • XPath checker-suggests XPath and can is used to test XPath results.
    • Firebug-xpath suggestions is just one of the many powerful features of this very useful add-on.
    • XPath helper-for Google Chrome
1.4. Locating Hyperlinks by Link Text

The use of this is know link text used within an anchor tag. With this strategy, the first element and the link text value matching the location would be returned. If no element has a matching link text attribute, a nosuchelementexception would be raised.

For instance, consider this page source:

The continue.html link can located like this:

Driver.  Find_element_by_link_text(' Continue ')driver.  Find_element_by_partial_link_text(' Conti ')         
1.5. Locating Elements by Tag Name

Use the If you want to locate a element by tag name. With this strategy, the first element with the given tag name would be returned. If no element has a matching tag name, a nosuchelementexception would be raised.

For instance, consider this page source:

The heading (h1) element can be located as this:

Driver.  Find_element_by_tag_name(' H1 ')    
1.6. Locating Elements by Class Name

The use of this is want to locate a element by class attribute name. With this strategy, the first element with the matching class attribute name would be returned. If no element has a matching class attribute name, a nosuchelementexception would be raised.

For instance, consider this page source:

The "P" element can be located as this:

Driver.  Find_element_by_class_name(' content ')    
1.7. Locating Elements by CSS selectors

The use of this is the want to locate a element by CSS selector Syntaxt. With this strategy, the first element with the matching CSS selector would be returned. If no element has a matching CSS selector, a nosuchelementexception would be raised.

For instance, consider this page source:

The "P" element can be located as this:

Driver.  Find_element_by_css_selector(' p.content ')    

Sauce Labs has the good documentation on CSS selectors.

Selenium-locating Elements

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.