博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
4. Locating Elements
阅读量:7098 次
发布时间:2019-06-28

本文共 7330 字,大约阅读时间需要 24 分钟。

There are various 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 will 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 are two private methods which might be useful with locators in page objects. These are the two private methods: find_element and find_elements.

Example usage:

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

These are the attributes available for 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. Locating by Id

Use this when you know id attribute of an element. With this strategy, the first element with the id attribute value matching the location will be returned. If no element has a matching id attribute, a NoSuchElementException will be raised.

For instance, consider this page source:

   

The form element can be located like this:

login_form = driver.find_element_by_id('loginForm')

4.2. Locating by Name

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

For instance, consider this page source:

   

The username & password elements can be located like this:

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

This will give the “Login” button as it occurs before the “Clear” button:

continue = driver.find_element_by_name('continue')

4.3. Locating by XPath

XPath is the language used for locating nodes in an XML document. As HTML can be an 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 sorts of new possibilities such as locating the third checkbox on the page.

One of the main reasons for using XPath is when you don’t have 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 have an id or name attribute. XPath locators can also be used to specify elements via attributes other than id and name.

Absolute XPaths contain the location of all elements from the root (html) and as a result are 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 based on the relationship. This is much less likely to change and can make your tests more robust.

For instance, consider this page source:

   

The form elements can be located 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']")
  1. Absolute path (would break if the HTML was 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 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. First form element with an 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 like this:

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. Input with attribute named name and the value continue and attribute named type and the value button
  2. Fourth input child element of the form element with attribute named id and value loginForm

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

  • - with interactive examples.

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

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

4.5. Locating Elements by Tag Name

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

For instance, consider this page source:

   

Welcome

Site content goes here.

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

heading1 = driver.find_element_by_tag_name('h1')

4.6. Locating Elements by Class Name

Use this when you want to locate an element by class attribute name. With this strategy, the first element with the matching class attribute name will be returned. If no element has a matching class attribute name, a NoSuchElementException will be raised.

For instance, consider this page source:

   

Site content goes here.

The “p” element can be located like this:

content = driver.find_element_by_class_name('content')

4.7. Locating Elements by CSS Selectors

Use this when you want to locate an element by CSS selector syntax. With this strategy, the first element with the matching CSS selector will be returned. If no element has a matching CSS selector, a NoSuchElementException will be raised.

For instance, consider this page source:

   

Site content goes here.

The “p” element can be located like this:

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

on CSS selectors.

转载于:https://www.cnblogs.com/kenfang/articles/5757141.html

你可能感兴趣的文章
我的友情链接
查看>>
64位操作系统不能安装64位虚拟机的解决办法
查看>>
怎样在log4j.xml配置文件中引入变量:小公司经验较多的我和阿里UC等大公司经验较多的Boss,一些技术交流和探讨...
查看>>
OpenJDK源码研究笔记(五)-缓存Integer等类型的频繁使用的数据和对象,大幅度提升性能(一道经典的Java笔试题)...
查看>>
什么是自然语言处理--学习笔记
查看>>
网闸中的核心技术
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
pythonic
查看>>
Ubuntu 配置 Android开发 adb调试
查看>>
OSX光标移动迟钝的原因
查看>>
Openstack Mitaka for Centos7.2 部署指南(三)
查看>>
Servlet的生命周期
查看>>
技术年货:美团技术沙龙合辑大放送——85个演讲,70+小时视频
查看>>
在人生方向感没那么强的时候,怎么更好地生活下去
查看>>
递归读取文件夹文件
查看>>
Select 1 from table 分析
查看>>
去掉文件夹的SVN属性
查看>>
Action 和 ActionSupport
查看>>
DHCP服务的应用
查看>>