Selenium Xpath Tutorials - Identifying xpath for element with examples to use in selenium
Let we try to understand how to identify Xpath of element with examples.
| 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.
| New Test | ||
| Command | Target | Value |
| open | http://www.wikipedia.org/ | |
| type | xpath= //body/div[3]/form/fieldset /input[2] |
Xpath Example1 |
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 |
| New Test | ||
| Command | Target | Value |
| open | http://www.wikipedia.org/ | |
| type | xpath= //body/div[3]/form/fieldset /input[@type='search'] |
Xpath Example3
|
| New Test | ||
| Command | Target | Value |
| open | http://www.wikipedia.org/ | |
| type | xpath= //body/div[3]/form/fieldset /input[@accesskey='F'] |
Xpath Example4
|
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.
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
|
//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的更多相关文章
- 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
- selenium之元素定位-xpath
被测试网页的HTML代码 <html> <body> <div id="div1" style="text-align:center&quo ...
- selenium+python自动化之xpath定位
在上一篇简单的介绍了用工具查看目标元素的xpath地址,工具查看比较死板,不够灵活,有时候直接复制粘贴会定位不到.这个时候就需要自己手动的去写xpath了,这一篇详细讲解xpath的一些语法. 什么是 ...
- selenium(9)- Xpath的详细使用
什么是Xpath 官方:XPath 是一门在 XML 文档中查找信息的语言.XPath 用于在 XML 文档中通过元素和属性进行导航 [XPath 使用路径表达式来选取 XML 文档中的节点或者节点集 ...
- selenium之坑(StaleElementReferenceException: Message: Element not found in the cache...)
有时候循环点击一列链接,只能点到第一个,第二个就失败了 原因是第二个已经是新页面,当然找不到之前页面的元素.就算是后退回来的,页面也是不一样的 页面长的一样不一定是同一张页面,就像两个人长的一样不一定 ...
- selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
抓取网页代码后,由于是在同一个li标签下,所以使用一次性抓取,所有的a标签,然后循环做不同的操作,但是抛出找不到元素异常. def office_page(_chrome: Chrome): sn = ...
- jQuery BlockUI Plugin Demo 4(Element Blocking Examples)
Element Blocking Examples This page demonstrates how to block selected elements on the page rather t ...
- Selenium常见报错问题(1)- 先来认识下selenium常见异常类
如果你在跑selenium脚本时,需要某些异常不知道怎么解决时,可以看看这一系列的文章,看看有没有你需要的答案 https://www.cnblogs.com/poloyy/category/1749 ...
- Python+Selenium 利用ID,XPath,tag name,link text,partial link text,class name,css,name定位元素
使用firefox浏览器,查看页面元素,我们以“百度网页”为示例 一.ID定位元素 利用find_element_by_id()方法来定位网页元素对象 ①.定位百度首页,输入框的元素 ②.编写示 ...
随机推荐
- highcharts .net导出服务 和 两种导出方式
highcharts 的Net导出服务 GitHub上整理的https://github.com/imclem/Highcharts-export-module-asp.net 引用两个程序集 sh ...
- java的反射
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制. ...
- zabbix利用api批量添加item,并且批量配置添加graph
关于zabbix的API见,zabbixAPI 1item批量添加 我是根据我这边的具体情况来做的,本来想在模板里面添加item,但是看了看API不支持,只是支持在host里面添加,所以我先在一个ho ...
- js 正则表达式 ( 1 )
https://regexper.com/ 量词: ?: 最多一次(0次或者1次) +:至少一次(大于等于1次) *:大于等于0次 {n}:n次 {n,m}:n次到m次之间,包括n,m {n,}:n次 ...
- git 远程仓库
1.查看当前的远程库 git remote git remote -v 2.添加远程仓库 git remote add [shortname] [url] 3.从远程仓库抓取数据 git fetch ...
- FlowLayoutPanel
动态生成控件 按顺序规律排列时 用panel的话 要指定特定的位置 .麻烦. 可以通过用flowLayoutPanel来解决. FlowLayoutPanel:表格布局面板,适合以表格形式规则的动态 ...
- JDK Collection 源码分析(3)—— Queue
@(JDK)[Queue] JDK Queue Queue:队列接口,对于数据的存取,提供了两种方式,一种失败会抛出异常,另一种则返回null或者false. 抛出异常的接口:add,remove ...
- Java多线程 3 线程同步
在之前,已经学习到了线程的创建和状态控制,但是每个线程之间几乎都没有什么太大的联系.可是有的时候,可能存在多个线程多同一个数据进行操作,这样,可能就会引用各种奇怪的问题.现在就来学习多线程对数据访问的 ...
- AutoComplete
aspx页面 需要引用的文件: <link rel="stylesheet" type="text/css" href="css/jquery. ...
- synthesize的作用
@synthesize是对属性的实现,实际上就是制定setter和getter操作的实例变量的名称 举个栗子: @synthesize array; 默认操作的实例变量和属性同名 @synthe ...