Selenium for Python. Chapter 4. Search for items

  • Tutorial
Continued translation of Selenium informal documentation for Python.
Translated with permission from Baiju Muthukadan.
The original can be found here .

Content:


1. Installation
2. First Steps
3. Navigation
4. Finding Elements
5. Expectations
6. Page Objects
7. WebDriver API
8. Application: Frequently Asked Questions

4. Search for items


There are a number of ways to search for items on a page. You have the right to use the most appropriate for specific tasks. Selenium provides the following methods for finding items on 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 all items that meet the search condition, use the following methods (list returned):

  • 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

[As you may have noticed, in the second list there is no search by id. This is due to the id property of HTML elements: page element identifiers are always unique. - Note per.]

In addition to the public methods listed above, there are two private methods that, when knowing the pointers of page objects, can be very useful: find_element and find_elements.

Usage example:

from selenium.webdriver.common.by import By
driver.find_element(By.XPATH, '//button[text()="Some text"]')
driver.find_elements(By.XPATH, '//button')

The following attributes are available for the By class:

ID = "id"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
NAME = "name"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"

4.1. Search by Id


Use this method when the id of an element is known. If no element matches the given id value, a NoSuchElementException will be thrown.

For example, consider the following page source code:


The form element can be defined as follows:

login_form = driver.find_element_by_id('loginForm')

4.2. Search by Name


Use this method when the name attribute of an element is known. The result will be the first element with the desired value of the name attribute. If no element matches the given name value, a NoSuchElementException will be thrown.

For example, consider the following page source code:


Elements with username and password can be defined as follows:

username = driver.find_element_by_name('username')
password = driver.find_element_by_name('password')

The following code will receive the “Login” button in front of the “Clear” button:

continue = driver.find_element_by_name('continue')

4.3. XPath Search


XPath is a language used to search for nodes in an XML document tree. Because HTML may be based on XML (XHTML), Selenium users are given the opportunity to search for elements in their web applications through this powerful language. XPath goes beyond simple search methods for the id or name attributes (and at the same time supports them), and opens up a range of new features, such as finding the third checkbox on a page, for example.

One good reason to use XPath is in situations where you cannot boast of attributes suitable as pointers, such as id or name, for the element you want to receive. You can use XPath to search for an element both in the absolute path (not recommended) and in the relative (for elements with given id or name). XPath pointers can also be used to define elements using attributes other than id and name.

The absolute XPath path contains all the tree nodes from the root (html) to the required element, and, as a result, is prone to errors as a result of the slightest adjustments to the source code of the page. If you find the closest element with the id or name attributes (ideally one of the parent elements), you can determine the element you are looking for using the parent-child relationship. These links will be much more stable and make your tests resistant to changes in the source code of the page.

For example, consider the following page source code:


The form element can be defined in the following ways:

login_form = driver.find_element_by_xpath("/html/body/form[1]")
login_form = driver.find_element_by_xpath("//form[1]")
login_form = driver.find_element_by_xpath("//form[@id='loginForm']")

  1. Absolute path (breaks at the slightest change in the structure of the HTML page)
  2. The first form element in an HTML page
  3. Form element for which an attribute is defined with the id name and loginForm value

The username element can be found like 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. The first form element with an input child element for which an attribute is defined with the name name and username value
  2. The first child of the input element of the form element, for which an attribute is defined with the id name and loginForm value
  3. The first input element for which an attribute is defined with the name name and username value

The “Clear” button can be found in the following ways:

clear_button = driver.find_element_by_xpath("//input[@name='continue'][@type='button']")
clear_button = driver.find_element_by_xpath("//form[@id='loginForm']/input[4]")

  1. An input element for which an attribute with the name name and value continue is set and an attribute with the name type and value button
  2. The fourth child input element of the form element, for which an attribute with the name id and the value loginForm is set

The examples presented cover some of the basics of using XPath, for a more in-depth study I recommend the following materials:


There are also a couple of very useful add-ons that can help you figure out the XPath element:

  • XPath Checker - Get XPath paths and can be used to check XPath path results
  • Firebug - getting the XPath path is just one of many powerful tools supported by this very useful plugin.
  • XPath Helper - for Google Chrome

4.4. Search for hyperlinks in hyperlink text


Use this method when the text inside the anchor tag is known [anchor tag, anchor tag, anchor tag - tag - Note. trans.]. Using this method, you will get the first element with the desired tag text value. If no item satisfies the search value, a NoSuchElementException will be thrown.

For example, consider the following page source code:

Are you sure you want to do this?

ContinueCancel

The hyperlink element with the address "continue.html" can be obtained as follows:

continue_link = driver.find_element_by_link_text('Continue')
continue_link = driver.find_element_by_partial_link_text('Conti')

4.5. Search for items by tag


Use this method when you want to find an element by its tag. In this way, you will get the first element with the specified tag name. If the search returns no results, a NoSuchElementException will be thrown.

For example, consider the following page source code:

Welcome

Site content goes here.


The header element h1 can be found as follows:

heading1 = driver.find_element_by_tag_name('h1')

4.6. Search for items by class


Use this method in cases when you want to find an element by the value of the class attribute. In this way you will get the first element with the desired class name. If the search fails, a NoSuchElementException will be thrown.

For example, consider the following page source code:

Site content goes here.


The “p” element can be found as follows:

content = driver.find_element_by_class_name('content')

4.7. Search for elements by CSS selector


Use this method when you want to get an element using the CSS selector syntax [CSS selector is a formal description of the relative path to the HTML element / elements. Classically, selectors are used to set style rules. In the case of WebDriver, the rules themselves are not necessary; the web driver uses CSS syntax only for search - Note trans.]. In this way, you get the first element matching the CSS selector. If no elements match the CSS selector, a NoSuchElementException will be thrown.

For example, consider the following page source code:

Site content goes here.


The “p” element can be defined as follows:

content = driver.find_element_by_css_selector('p.content')

Sauce Labs has some good CSS selector documentation .
From a translator: I also advise you to refer to the following materials:


Go to next chapter

Also popular now: