Selenium Python Tutorial Chapter 4

insert image description here

4. Find elements

There are many different strategies for positioning an element on a page. In your project, you can choose the most appropriate method to find elements. Selenium provides the following methods for you:

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

Find multiple elements at once (these methods 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

In addition to the above public methods, there are two private methods below, which may be useful when you are looking for page elements. They are 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')

Here are some of the available properties of 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. Find element by ID
When you know the id of an element, you can use this method. Under this policy, the first element with this id in the page will be matched and returned. If no element is found, a NoSuchElementException is thrown.

As an example, a page element looks like this:

<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>

You can find form elements like this:

login_form = driver.find_element_by_id('loginForm')

4.2. Find elements by Name

You can use this method when you know the name of an element. Under this policy, the first element of the name in the page will be matched and returned. If no element is found, a NoSuchElementException is thrown.

As an example, a page element looks like this:

<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 whose name attributes are username & password can be found like this:

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

This will get the "Login" button because it is before the "Clear" button:

continue = driver.find_element_by_name('continue')

4.3. Finding elements by XPath

XPath is a syntax for finding nodes in an XML document. Because HTML documents can also be converted into XML (XHTML) documents, Selenium users can take advantage of this powerful language to find elements in web applications. XPath extends (and of course supports) this simple way of getting an element by its id or name attribute, and it also opens up all sorts of new possibilities, such as getting the third checkbox on a page.

One of the main reasons to use XPath is when you want to get an element that has neither an id attribute nor a name attribute, you can use XPath to get him using the absolute position of the element (this is not recommended), or relative to having a id or name attribute of the element (theoretically the parent element) to get the element you want. XPath locators can also find elements by non-id and name attributes.

Absolute XPath means that all elements are positioned from the position of the root element (HTML). As long as there is a slight adjustment in the application, your positioning will fail. But by positioning your elements from the nearest element containing the id or name attribute, this relative relationship is very reliable, because this positional relationship rarely changes, so it can make your test more powerful.

As an example, a page element looks like this:

<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>

You can find form elements like this:

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']")

Absolute positioning (slightly adjusting the page structure will be destroyed)
The first form element in the HTML page
contains the id attribute and the form element
username whose value is loginForm can be obtained as follows:

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']")

The first form element contains the name attribute and the input element whose value is username and whose id
is loginForm. The first input child element of the form element
whose name attribute is username. The first input element
"Clear" button can be obtained as follows:

clear_button = driver.find_element_by_xpath("//input[@name='continue'][@type='button']")
clear_button = driver.find_element_by_xpath("//form[@id='loginForm']/input[4]")
Input with attribute named name and the value continue and attribute named type and the value button
Fourth input child element of the form element with attribute named id and value loginForm

There are also some very useful plugins that can assist in discovering the XPath of elements:

XPath Checker - suggests XPath and can be used to test XPath results.
Firebug - XPath suggestions are just one of the many powerful features of this very useful add-on.
XPath Helper - for Google Chrome

4.4. Get hyperlinks by link text

Use this when you know the link text to use in an anchor tag. Under this strategy, the first matching link content anchor tag in the page will be matched and returned. If no element is found, a NoSuchElementException is thrown.

As an example, a page element looks like this:

<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 continue.html hyperlink can be found like this:

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

4.5. Find elements by tag name

Use this when you want to find elements by tag name. Under this strategy, the first element on the page that matches the tag name will be matched and returned. If no element is found, a NoSuchElementException is thrown.

As an example, a page element looks like this:

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

The h1 element can be looked up as follows:

heading1 = driver.find_element_by_tag_name('h1')

4.6. Locating elements by Class name

Use this when you want to find elements by class name. Under this strategy, the first element on the page that matches the class attribute will be matched and returned. If no element is found, a NoSuchElementException is thrown.

As an example, a page element looks like this:

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

The p element can be looked up as follows:

content = driver.find_element_by_class_name('content')

4.7. Finding elements by CSS selectors

Use this when you want to find elements via CSS selectors. Under this strategy, the first element on the page that matches the CSS selector will be matched and returned. If no element is found, a NoSuchElementException is thrown.

As an example, a page element looks like this:

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

The p element can be looked up as follows:

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

Guess you like

Origin blog.csdn.net/captain5339/article/details/131105074