📜 ⬆️ ⬇️

Selenium for Python. Chapter 4. Search for items

Continuation of the translation of the unofficial Selenium documentation for Python.
The translation was made with the permission of the author Baiju Muthukadan.
The original can be found here .

Content:


1. Installation
2. First Steps
3. Navigation
4. Search for Items
5. Expectations
6. Page Objects
7. WebDriver API
8. Appendix: Frequently Asked Questions

4. Search for items


There are a number of ways to search for items on a page. You may use the most relevant for specific tasks. Selenium provides the following methods for finding items on the page:


To find all the items that match the search term, use the following methods (a list is returned):
')

[As you can see, in the second list there is no search by id. This is due to the id property of HTML elements: the page element identifiers are always unique. - Approx. trans.]

In addition to the public (public) methods listed above, there are two private (private) methods that, if you know 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 the element is known. If no element satisfies the specified id value, a NoSuchElementException will be thrown.

For example, consider the following page source code:

 <html> <body> <form id="loginForm"> <input name="username" type="text" /> <input name="password" type="password" /> <input name="continue" type="submit" value="Login" /> </form> </body> <html> 

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 element's name attribute is known. The result will be the first element with the desired value of the name attribute. If no element satisfies the specified name, a NoSuchElementException will be thrown.

For example, consider the following page source code:

 <html> <body> <form id="loginForm"> <input name="username" type="text" /> <input name="password" type="password" /> <input name="continue" type="submit" value="Login" /> <input name="continue" type="button" value="Clear" /> </form> </body> <html> 

Elements named 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 get the “Login” button in front of the “Clear” button:

 continue = driver.find_element_by_name('continue') 

4.3. XPath Search


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

One of the good reasons for using XPath is that there are situations where you cannot brag about usable attributes as pointers, such as id or name, for the element you want to receive. You can use XPath to search for an element either by absolute path (not recommended) or relative (for elements with given id or name). XPath pointers, among others, can be used to define elements using attributes other than id and name.

The absolute path of the XPath contains all the nodes of the tree 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 attributes id or name (ideally one of the parent elements), you can determine the desired element using the parent-slave relationship. These links will be much more stable and will make your tests resistant to changes in the source code of the page.

For example, consider the following page source code:

 <html> <body> <form id="loginForm"> <input name="username" type="text" /> <input name="password" type="password" /> <input name="continue" type="submit" value="Login" /> <input name="continue" type="button" value="Clear" /> </form> </body> <html> 

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 the HTML page
  3. The form element for which an attribute is defined with the name id and the value of the loginForm

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

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 is specified with the name name and the value of continue and an attribute with the name type and the value of button
  2. The fourth child element of the input of the form element, for which an attribute is specified with the name id and the value of loginForm

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


There are also a couple of very useful add-ons that can help in determining the XPath element:


4.4. Search for hyperlinks by hyperlink text


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

For example, consider the following page source code:

 <html> <body> <p>Are you sure you want to do this?</p> <a href="continue.html">Continue</a> <a href="cancel.html">Cancel</a> </body> <html> 

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 item by its tag. In this way, you will get the first element with the specified tag name. If the search fails, a NoSuchElementException will be thrown.

For example, consider the following page source code:

 <html> <body> <h1>Welcome</h1> <p>Site content goes here.</p> </body> <html> 

The h1 header element can be found as follows:

 heading1 = driver.find_element_by_tag_name('h1') 

4.6. Search for items by class


Use this method when you want to find an element by the value of the class attribute. In this way, you 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:

 <html> <body> <p class="content">Site content goes here.</p> </body> <html> 

The element “p” can be found as follows:

 content = driver.find_element_by_class_name('content') 

4.7. Search elements by CSS selector


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

For example, consider the following page source code:

 <html> <body> <p class="content">Site content goes here.</p> </body> <html> 

The element “p” can be defined as follows:

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

Sauce Labs has good documentation for CSS selectors.
From the translator: I advise you also to refer to the following materials:


Go to the next chapter.

Source: https://habr.com/ru/post/250975/


All Articles