An Introduction to XPath in Selenium WebDriver
source link: https://www.tuicool.com/articles/r6NZfui
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Test automation is booming in the IT industry, and it wouldn't be too far-fetched to begin calling automation itself an industry. However, we're not so far ahead that there will be a less manual intervention in the functional testing of any product. There are lots of test automation tools in the industry and the one which is leading the pack isSelenium WebDriver. It's not just because it's open source, but because of the versatility it carries. Selenium does not require an in-depth introduction as it should be familiar to every QA expert.
We identify WebElements through different locators on the web page when we are using Selenium WebDriver. Some of the locators are mentioned below:
- ID
- Name
- Class Name
- Link Text
- Tag Name
- Partial Link Text
- CSS Selector
- XPath
These eight locators are used to identify objects on any UI to perform testing operations with Selenium WebDriver. Among these, XPath is the most important one as it helps to handle dynamic elements.
Types of XPath
There are two main types of XPath that are used in Selenium:
- Absolute XPath: The XPath is designed with a single slash ("/") and it starts from the top of the DOM. If any change in position of the elements occurs, then it will lead to a failure in object identification. and example is: html/head/body/table/tbody/thead/tr/th
- Relative XPath: This type of XPath is used to handle dynamic objects efficiently. It is designed with a double slash ("//"), along with some methods to identify objects relatively. An example is: //input[@id='some_id']
How to Create a Relative XPath to Identify Dynamic Objects
There are some ways to create a relative XPath which further helps to identify frequently changing objects on web UI.
Let's see the ways to create a relative XPath.
Format: //tagname[@attribute='value']
This is the format of creating a Relative XPath. For example, let's go to my DZone profile page . I need to write a Relative XPath to identify my name. See screenshot below:
If you look at the screenshot above, you notice that I used div
as thetagname, class
as the attribute and user-name
as the value of the attribute. This creates a complete XPath that looks like this: //div[@class='user-name']
We can identify elements through the axis as well by using some methods like following, preceding, parent, following-sibling, self, contains, starts-with, and ends-with.
Recommend
-
130
jest-webdriver - Connect Jest tests to Selenium WebDriver
-
63
本人用的是python3.63版本这里会讲到安装时所遇到的问题这里是根据火狐浏览器来操作的1、首先安装selenium在windows的cmd窗口通过pip没安装pip的小伙伴自行去搜搜相关pip安装方法pipinstallselenium安装成功后测试一下是否能正常运行打开百度输入关键字并且搜索fro...
-
48
-
27
Selenium WebDriver wrapper Framework which supports Automation of most of the web browsers. Detailed documentation on m...
-
27
README.md php-webdriver – Selenium WebDriver bindings for PHP
-
18
元素的定位和操作是自动化测试的核心部分,其中操作又是建立在定位的基础上的。 本文以百度页面为例子,带入深入了解XPath和CSS定位的所有方法,代码较多,文字只提示重点关注的地方!!! 浏览器的常规操作
-
19
【51CTO.com快译】Selenium是用于Web应用程序测试的最广泛的自动化框架之一。它可以通过自动执行浏览器测试,以简化我们花费在质量检查上的人工测试工作。Selenium WebDriver是Selenium套件中的一个关键组件。本文将和您讨论什么是Seleniu...
-
10
Setting Http Proxy when using the selenium-webdriver gem and Firefox July 14, 2010 I've been using the selenium-webdriver ruby gem to do some automated...
-
1
Webdriver how to find the parent field set element of a given element using Xpath advertisements I have a HTML like the one below
-
5
说明:在HTML页面中,<p> 是一个标签,<p>hello</p> 是一个元素,元素由一个开始的标签和结束的标签组成。<font color="red">标签包含属性名color,属性值为red。 说明:CSS的id选择器,id常用于JS操作DOM节点对象。当同个id被多次...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK