Xpath in selenium is close to must required. XPath is element locator and you need to provide xpath during selenium test script creation. You need to provide any element locator(like id, name, css path, xpath etc.) in target column of selenium IDE software testing tool's window to locate that specific element to perform some action on it and you are already aware about that. In previous post, we have learn about how to identify element id or name of software web application's element . If you have worked with selenium IDE software testing tool then you knows that sometimes elements does not contains id or name. Locating element by Xpath in selenium is the another way of locating element and you can use it as a alternative of id or name of element.

 
 
What Is XPath?
Xpath in XML document shows the direction of software web application's element location through nodes and attributes.

 
 

Let we try to understand how to identify Xpath of element with examples.

 
 
 
Above given image is taken from http://www.wikipedia.org/. It is firebug view of page. You can read THIS POST to know how to install firebug and firepath in firefox browser and THIS POST will describe you how to use it. Look into the image there are three fields 1. Input text box 2. select drop down and 3. input button. And bellow of those fields there is expansion of relative XML nodes through firebug. As you see in image, you can use "id=searchInput" or "name=search" to identify input text box to type something in to it as bellow given example.
New Test
Command Target Value
open http://www.wikipedia.org/  
type id=searchInput ID Example
 

or

New Test
Command Target Value
open http://www.wikipedia.org/  
type name=search Name Example

Xpath in selenium Tutorial

Now if you want to identify same element (input textbox) with xpath then you can use any of the bellow given syntax in to the target column with type command in above example.

 
Locating element using Xpath in selenium with Examples for input text box
 
1. Identifying Xpath using full path of XML
xpath=//body/div[3]/form/fieldset/input[2]  //// Here //body is the main root node, /div[3] describes the 3rd div child node of parent node body, /form describes the child node form of parent node div[3], /fieldset describes the child node fieldset of parent node form, /input[2] describes the 2nd input child node of parent node fieldset.
New Test
Command Target Value
open http://www.wikipedia.org/  
type xpath=
//body/div[3]/form/fieldset
/input[2]
Xpath
Example1

 
 
2. Writting Xpath using last()
xpath=//body/div[3]/form/fieldset/input[last()-2]  //// Here /input[last()-2] describes the 3rd upper input node(input[2]) from last input node.
xpath=//body/div[3]/form/fieldset/*[last()-3]   //// Here /*[last()-3] describes the 4th upper  node(input[2]) from last node.

New Test
Command Target Value
open http://www.wikipedia.org/  
type xpath=
//body/div[3]/form/fieldset
/input[last()-2]
Xpath
Example2

 
 
3. Xpath locator using @ and attribute
xpath=//body/div[3]/form/fieldset/input[@type='search']  //// Here /input[@type='search'] describes the input node having attribute type='search'.
New Test
Command Target Value
open http://www.wikipedia.org/  
type xpath=
//body/div[3]/form/fieldset
/input[@type='search']
Xpath Example3

 
 
4. Xpath expression using @ and attribute
xpath=//body/div[3]/form/fieldset/input[@accesskey='F']  //// Here /input[@accesskey='F'] describes the input node having attribute @accesskey='F'. Another way of same is as bellow.

New Test
Command Target Value
open http://www.wikipedia.org/  
type xpath=
//body/div[3]/form/fieldset
/input[@accesskey='F']
Xpath Example4

 

5. Xpath in selenium using @ and attribute
xpath=//input[@accesskey='F']  //// Here //input[@accesskey='F'] describes the input node having attribute @accesskey='F'. Try it by using it in above example.
 
6. Xpath example using @ and attribute
xpath=//input[@type='search']   //// Here /input[@type='search'] describes the input node having attribute type='search'. Try it by using it in above example.
 
7. XML Xpath using /descendant:: keyword
xpath=//div[@class='search-container']/descendant::input[@accesskey='F']   //// Here i have used descendant in between. In this case i have described only starting node div with attribute class='search-container' and final node input with accesskey='F' attribute. So not need to describe in between nodes. Try it by using it in above example.
 
8. Xpath query example using contains keyword
xpath=//input[contains(@id, "searchInput")]   ////Here i have used contains keyword to identify id attribute with text "searchInput". Try it by using it in above example.
 
9. xpath using and with attributes
xpath=//input[contains(@id, "searchInput") and contains(@accesskey,"F")]   ////In this example, It will look at two attributes in input node. Try it by using it in above example.
 
10. XML xpath value value using position()
xpath=//div[@class='search-container']/descendant::input[position()=2]   ////This xpath will select input node which is on number 2 position and it is for input text box as shown in image. Try it by using it in above example.

11. Using starts-with keyword
xpath=//input[starts-with(@type, "s")]    ////   In this example, It will find input node with attribute is 'type' and its value is starting with 's' (here it will get type = 'search').

12. Using OR (|) condition with xpath
xpath=//input[@accesskey='F'] | //input[@id='searchInput']
xpath=//input[@accesskey='F' or @id='searchInput']  //// In both these example, it will find input text box with accesskey='F' or @id='searchInput'. If any one found then it will locate it. Very useful when elements appears alternatively.

13. Using wildcard * with to finding element xpath
xpath=//*[@accesskey='F'] 

14. Finding nth child element of parent
xpath=//body/*[3]/form/fieldset/*[2]    ////This xpath is for search text box. Here, /*[3] describes the 3rd child element of body which is div[3]. Same way *[2] describes the 2nd child element of fieldset which is input[2]

All above examples are for input text box. Now let me write Xpath for drop down.
 
Xpath Examples for drop down
1. xpath=//body/div[3]/form/fieldset/select
2. xpath=//body/div[3]/form/fieldset/select[last()]
3. xpath=//body/div[3]/form/fieldset/select[@id='searchLanguage']
4. xpath=//body/div[3]/form/fieldset/select[@name='language']
5. xpath=//div[@class='search-container']/descendant::select[@name='language']
6. xpath=//select[contains(@id, "searchLanguage")]
7. xpath=//div[@class='search-container']/descendant::select[position()=1]
8. xpath=//body/div[3]/form/fieldset/select[count(*)>1]


New Test
Command Target Value
open http://www.wikipedia.org/  
select xpath=//div[@class='search-container']/descendant::select[position()=1] label=English

 
Other Xpath Example
1. Finding xpath in selenium for target link 'url'
//a[@href='//meta.wikimedia.org/wiki/List_of_Wikipedias']  ////This xpath example will find link with given URL (//meta.wikimedia.org/wiki/List_of_Wikipedias) on the page.

2. Finding xpath of element with no child
xpath=//img[count(*)=0]   ////This xpath is for wikipedia text logo which is display on top of the page. This xpath will find that image element which have not any child element. Here image node is last and it has not any child element.

xpath=//div[2]/descendant::img[count(*)=0]   //// This xpath is for wikipedia logo image which is display under logo text. 

Selenium Xpath Tutorials - Identifying xpath for element with examples to use in selenium的更多相关文章

  1. How to get the xpath by clicking an html element

    How to get the xpath by clicking an html element How to get the xpath by clicking an html element

  2. selenium之元素定位-xpath

    被测试网页的HTML代码 <html> <body> <div id="div1" style="text-align:center&quo ...

  3. selenium+python自动化之xpath定位

    在上一篇简单的介绍了用工具查看目标元素的xpath地址,工具查看比较死板,不够灵活,有时候直接复制粘贴会定位不到.这个时候就需要自己手动的去写xpath了,这一篇详细讲解xpath的一些语法. 什么是 ...

  4. selenium(9)- Xpath的详细使用

    什么是Xpath 官方:XPath 是一门在 XML 文档中查找信息的语言.XPath 用于在 XML 文档中通过元素和属性进行导航 [XPath 使用路径表达式来选取 XML 文档中的节点或者节点集 ...

  5. selenium之坑(StaleElementReferenceException: Message: Element not found in the cache...)

    有时候循环点击一列链接,只能点到第一个,第二个就失败了 原因是第二个已经是新页面,当然找不到之前页面的元素.就算是后退回来的,页面也是不一样的 页面长的一样不一定是同一张页面,就像两个人长的一样不一定 ...

  6. selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

    抓取网页代码后,由于是在同一个li标签下,所以使用一次性抓取,所有的a标签,然后循环做不同的操作,但是抛出找不到元素异常. def office_page(_chrome: Chrome): sn = ...

  7. jQuery BlockUI Plugin Demo 4(Element Blocking Examples)

    Element Blocking Examples This page demonstrates how to block selected elements on the page rather t ...

  8. Selenium常见报错问题(1)- 先来认识下selenium常见异常类

    如果你在跑selenium脚本时,需要某些异常不知道怎么解决时,可以看看这一系列的文章,看看有没有你需要的答案 https://www.cnblogs.com/poloyy/category/1749 ...

  9. Python+Selenium 利用ID,XPath,tag name,link text,partial link text,class name,css,name定位元素

    使用firefox浏览器,查看页面元素,我们以“百度网页”为示例 一.ID定位元素    利用find_element_by_id()方法来定位网页元素对象 ①.定位百度首页,输入框的元素 ②.编写示 ...

随机推荐

  1. 【C#】安装windows服务

    参考:http://blog.csdn.net ,http://blog.csdn.net/dyzcode 1.新建 visual studio insaller 项目2.添加 [文件系统]3.添加 ...

  2. JFinal 项目 在tomcat下部署

    原文:http://my.oschina.net/jfinal/blog/353062 首先明确一下 JFinal 项目是标准的 java web 项目,其部署方式与普通 java web 项目没有任 ...

  3. Android----消息弹出框

    关于Android的知识,自从工作了就没有什么时间去总结学习过的知识,我个人比较喜欢学习后总结,今天就写一下关于android中消息弹出框的几种方式的简单示例,按照自己的思路写了一段,希望对和我一样在 ...

  4. PM成长之路(一)

    到底什么样的人适合任项目经理一直是很多企业的困惑,因为大家发现优秀项目经理的特质看起来和传统的职能经理或技术专家很不一样.当企业在决定开展一个重大的项目时,如果不能找到一个适合带领和管理项目的项目经理 ...

  5. 【Beta】Scrum09

    Info 考试周,暂停工作 时间:2016.12.26 21:35 时长:20min 地点:大运村1号公寓5楼楼道 类型:日常Scrum会议 NXT:2016.12.31 21:30 Task Rep ...

  6. MSSQLServer 纵向表转横向表 横向表转纵向表 行转列 列转行

    MSSQLServer 纵向表转横向表  横向表转纵向表 建表语句及插入数据语句: CREATE TABLE Test_y( ) NULL, ) NULL, [Grade] [int] NULL ) ...

  7. jQuery 下拉框应用 拓展

    jquery 书本上的一个例子 书本上只写了从左边添加到右边,无非就是remove() 方法和 appendTo() 方法. 然而,我试过了,并不能像从左边添加到右边那样简单的把右边的删除到左边过来. ...

  8. Java虚拟机(JVM)以及跨平台原理详细的介绍

    相信大家已经了解到Java具有跨平台的特性,可以"一次编译,到处运行",在Windows下编写的程序,无需任何修改就可以在Linux下运行,这是C和C++很难做到的.那么,跨平台是 ...

  9. jQuery选择器总结

    jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法   $("#myELement")    选择id值等于myElement的元素,id值不能重复在文档中 ...

  10. iOS视频边下边播--缓存播放数据流

    实现视频边下边播,这里的边下边播不是单独开一个子线程去下载,而是把视频播放的数据给保存到本地.简而言之,就是使用一遍的流量,既播放了视频,也保存了视频. 用到的框架:<AVFoundation/ ...