SoFunction
Updated on 2024-11-14

Example of Selenium Positioning

This article example describes the Selenium positioning element operation. Shared for your reference, as follows:

Selenium is a tool for web application testing.Selenium tests run directly in the browser, as if the real user is operating. Supported browsers include IE (7, 8, 9, 10, 11), Mozilla Firefox, Safari, Google Chrome, Opera and more. The main features of this tool include: testing compatibility with browsers - test your application to see if it works well on top of different browsers and operating systems. Test system functionality - create regression tests to verify software functionality and user requirements. Net, Java, Perl and other languages to automatically generate test scripts.

In a previous post, theBasic Selenium UsageIn the "Simple record of the use of Selenium, the next record of the positioning of elements, Selenium provides a variety of strategies to locate the elements in the page, Selenium provides the following methods to locate the elements in the page, you can according to their own preferences to make a choice:

  • find_element_by_id: Match lookup by ID, returns only one matched element
  • find_element_by_name: Match lookup by name, returns only one matched element.
  • find_element_by_xpath: Match lookup by xpath, returns only one matched element
  • find_element_by_link_text: Match search by link content, return only one matched element
  • find_element_by_partical_link_text: Match search by partial link content, return only one matched element
  • find_element_by_tag_name: Match lookup by tag name, returns only one matched element
  • find_element_by_class_name: Match lookup by class name, return only one matched element
  • find_element_by_css_selector: Match-finding via CSS selectors, returning only one matched element

It's worth noting that the above methods will only match to find only the first element. In addition to the above methods for finding a single element, Selenium also defines methods for finding multiple elements:

  • find_elements_by_name: Match by name, returns a list of all matched elements.
  • find_elements_by_xpath: Match lookup by xpath, returns a list of all matched elements.
  • find_elements_by_link_text: Match the content of the link to find, return a list of all matched elements
  • find_elements_by_partical_link_text: Match the content of the link through part of the search, return a list of all matched elements
  • find_elements_by_tag_name: Match lookup by tag name, return a list of all matched elements
  • find_elements_by_class_name: Match by class name and return a list of all matched elements.
  • find_elements_by_css_selector: Finds matches by CSS selector, returns a list of all matched elements.

In addition to the public methods given above, Selenium provides two private methods that may be useful for locators in page objects, these are:find_elementcap (a poem)find_elements

from  import By
element = driver.find_element(,'//*[@]')
elements = driver.find_elements(,'//button')

followingByAvailable Properties:

ID = 'id'
NAME = 'name'
XPATH = 'xpath'
LINK_TEXT = 'Link content'
PARTIAL_LINK_TEXT = 'Partial link content'
TAG_NAME = 'Tag name'
CLASS_NAME = 'Class name'
CSS_SELECTOR = 'CSS Selector'

Localization by ID

You can use the ID attribute of an element when you know it, using this strategy will return the first element whose ID attribute value matches that. If no element matches this ID attribute, it will raise theNoSuchElementExceptionError, for example, there is this data source:

<input  type="text"/>

We know the ID of this element and we can position it like this:

element = driver.find_element_by_id('login_id')

Locate by name

You can use the name attribute of an element when you know it, using this strategy will return the name attribute value of the first element that matches it. If no element matches this name attribute, it will raise theNoSuchElementExceptionError, for example, there is this data source:

<input name="login" type="text"/>

We know the element'sname, we can position ourselves in this way:

element = driver.find_element_by_name('login')

Positioning via XPath

XPath is the language used to find nodes in XML documents, and since XML can be an implementation of HTML, Selenium users can take advantage of this powerful language to locate elements in their web applications.XPath extends the simple method of locating by id and name attributes and opens up all sorts of new possibilities, such as looking for the third checkbox on a page. As a chestnut, there is this data source:

<html>
 <body>
 <form >
  <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>

We can position ourselves like this:

user_element = driver.find_element_by_xpath("/html/body/form/input[@name='username']")

or

user_element = driver.find_element_by_xpath("/html/body/form/input")

or

user_element = driver.find_element_by_xpath('//input[1]')

or

user_element = driver.find_element_by_xpath("//input[@name='username'][@type='text']")

Targeting by link content

You can use it when you know the content of the linked element, using this strategy will return the link content with the first element that this matches to. If no element matches to this link content, it will raise theNoSuchElementExceptionError, for example, there is this data source:

<html>
 <body>
 <p>Are you sure you want to do this?</p>
 <a href="" rel="external nofollow" rel="external nofollow" >Continue</a>
 <a href="" rel="external nofollow" >Cancel</a>
</body>
<html>

included among these<a href="" rel="external nofollow" rel="external nofollow" >Continue</a>Elements can be positioned like this:

element = driver.find_element_by_link_text('Continue')

or

element = driver.find_element_by_partical_link_text('Con')

Locate by tag name

You can use it when you know the name of the tag, using this strategy will return the tag name with the first element that matches to that. If no element matches this tag name, it will raise theNoSuchElementExceptionError, for example, there is this data source:

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

We can do this to<p>element for positioning:

element = driver.find_element_by_tag_name("p")

Localization by class name

You can use it when you know the class name, using this strategy will return the class name with the first element that matches that. If no element matches the class name, it will raise theNoSuchElementExceptionError, for example, there is this data source:

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

We can do this to<p>element for positioning:

element = driver.find_element_by_class_name('content')

Positioning via CSS selectors

You can use it when you want to find an element by CSS selector syntax, using this strategy will return the first element that matches the CSS selector. When no element matches this CSS selector, it will raise theNoSuchElementExceptionError, for example, there is this data source:

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

We can do this to<p>element for positioning:

element = driver.find_element_by_css_selector('body p')

Readers interested in more Python related content can check out this site's topic: thePython Socket Programming Tips Summary》、《Summary of Python URL manipulation techniques》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniquesand thePython introductory and advanced classic tutorials

I hope the description of this article will help you in Python programming.