Xpath Student Notest
Xpath Student Notest
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
LOCATORS
-->Locators are used to get the location of the WebElements in a webpage (html
document).
-->Based on the requirement we need to find the exact location of the webelements.
-->All the locators are static methods of "By" class.
This "By" class is an abstract class,so it is not possible to create the
object.(If we try we get
C.T.E)
Hence the only way to access the locators is by using
*classname.method(locators--static methods)
Types of Locators
-----------------
1) id()
2) name()
3) classname()
4) linktext()
5) partialLinkText()
6) cssSelector()
7) xpath()
8) tagname()
1. id(String idvalue)
------------------
-->It is locator(static method) present in a "By class",which is used to locate the
webelement by using id attribute value.
===================================================================================
========================
2. name(String namevalue)
----------------------
-->It is a locator(static method) present in a "By class",which is used to locate
the webelement by using name attribute vaue.
===================================================================================
=======================
3. classname(String clasvalue)
---------------------------
-->It is a locator(static method) present in a "By class",which is used to locate
the webelement by using class attribute value.
Ex:- If class=abc def ghi jkl 123xyz abc -
2....class3,class4
note**
If class attribute is having spaces than it is considered as multiple classes.
===================================================================================
========================
4. linktext(String complete_visible_text)
------------------------------------
-->It is a locator(static method) present in a "By class",which is used to locate
the webelement by using complete_visible_text of the link.
===================================================================================
======================
5. partial linktext(String partial_visible_text)
----------------------------------------------
-->It is a locator(static method) present in a "By class",which is used to locate
the webelement by using partial_visible_text of the link.
===================================================================================
======================++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6. CSS selector(String selector value)
-----------------------------------
There are even shortcuts to create CSS Expression for class and id.
Ex1:-
[id='username'] shortcut is.... #username
input[id='username'] cssSelector(#username)
input[id='username'] shortcut is.... input#username
Ex2:-
[class='pwdfield'] shortcut is.... .pwdfield
input[class='pwdfield'] shortcut is.... input.pwdfield
// htmltag[AttributeName='AttributeValue']
//
driver.findElement(By.cssSelector("input[id='username']")).sendKeys("admin");
driver.findElement(By.cssSelector("input#username")).sendKeys("admin");
// htmltag[AttributeName='AttributeValue']
// driver.findElement(By.cssSelector("input[class='textField
pwdfield']")).sendKeys("manager");
driver.findElement(By.cssSelector("input.pwdfield")).sendKeys("manager");
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
===================================================================================
===========================
note**
-----
Priority of locators to use
===========================
a) For other than links
id()>name()>classname()>CSSselector() If nothing available in html,we go
for xpath to locate webelement
b) For links
id()>name()>classname()>linktext()>partiallinktext()>CSSselector()
===================================================================================
============================
7.Xpath()
-------
-->It is a locator(static method) present in a "By class",which is used to locate
the webelement by using any
html tag or any attributr or any visible text.
XPATH: (xml-Path)
=======
It is a path of an element in html tree structure.
*** To traverse from parent to immediate child we make use of /(forward slash)
1<html>
1<body>
1<div>
1<input type="text" value="A">
2<input type="text" value="B">
</div>
2<div>
1<input type="text" value="C">
2<input type="text" value="D">
</div>
</body>
</html>
WebElements X=Path
------------ -------
A html/body/div[1]/input[1]
B html/body/div[1]/input[2]
C html/body/div[2]/input[1]
D html/body/div[2]/input[2]
AB html/body/div[1]/input
CD html/body/div[2]/input
AC html/body/div/input[1]
BD html/body/div/input[2]
AD html/body/div[1]/input[1]|html/body/div[2]/input[2]
BC html/body/div[1]/input[2]|html/body/div[2]/input[1]
ABCD html/body/div/input
ABC html/body/div[1]/input|html/body/div[2]/input[1]
BDC html/body/div[1]/input[2]|html/body/div[2]/input
Writing the xpath expression from beginning of the html tree till element is called
as Absolute x-path.
Note:For n number of tags we can use n-1 '/' single forward slash
b)Relative Xpath
---------------
-->// -->Double forward slash (Traverse from parent to any child)
-->/ -->Single forward slash (Traverse from parent to immediate child)
To follow this kind of approach also we required html tree structure which is time
consuming and different.
In order to overcome this approach,there are several cases of xpath which makes
relative xpath more accurate and efficient.
1) Xpath by attribute
------------------
//html tag[@Attributename='Attribute value'] @->Search given value only in
attribute.
2) Xpath by VisibleText
--------------------
//html tag[text()='Complete_visible_text'] text()->It is xpath function
which searchgiven value only
in visible text.
3) Xpath by contains Attribute
---------------------------
//html tag[contains(@Attribute name,'Partial Attribute Value')]
//xpath by Attribute
//htmltag[@AttributeName='AttributeValue']
driver.findElement(By.xpath("//input[@placeholder='Username']")).sendKeys("admin");
//
driver.findElement(By.xpath("//input[@placeholder='Password']")).sendKeys("manager"
);
//xpath by visibleText()
//htmltag[text()='AttributeValue']
// driver.findElement(By.xpath("//div[text()='Login ']")).click();
//driver.findElement(By.xpath("//div[.='Login ']")).click();
driver.findElement(By.xpath("//input[contains(@class,'pwdfield')]")).sendKeys("mana
ger");
driver.findElement(By.xpath("//td[contains(@id,'loginB')]")).click();
Thread.sleep(1000);
//xpath by visible text
//driver.findElement(By.xpath("//a[text()='View Time-Track']")).click();
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
And
a b y
0 0 0
0 1 0
1 0 0
1 1 1
OR
a b y
0 0 0
0 1 1
1 0 1
1 1 1
Syntax:
-------
//tagName[@AttributeName='AttributeValue' and @AttributeName='AttributeValue']
//tagName[@AttributeName='AttributeValue' or @AttributeName='AttributeValue']
AND--> both the locator should be correct
OR---> any one of the locator should be correct
Ex://input[@type='text' or @id='email']
Ex://input[@type='text' and @id='email']
//using multipleAttribute
driver.findElement(By.xpath("//input[@id=\"username\" and
@class=\"textField\"]")).sendKeys("admin");
driver.findElement(By.xpath("//input[@placeholder=\"Password\" or
@name=\"pwd\"]")).sendKeys("manager");
Syntax:
-------
//htmltag[text()='textValue']
or
//htmltag[.='textValue']
Syntax:
Synatax:
--------
//tagName[contains(text(),'textValue')]
or
//tagName[contains(.,'textValue')]
Synatx: //tagName[.='TextValue']/../tag[index]
-------
<html>
<body>
<table border="1">
<tbody>
<tr>
<td>1</td>
<td>Kanthara</td>
<td>10cr</td>
</tr>
<tr>
<td>2</td>
<td>KGF2</td>
<td>25cr</td>
</tr>
<tr>
<td>3</td>
<td>RRR</td>
<td>20</td>
</tr>
</tbody>
</table>
</body>
</html>
Tree Structure:
--------------
html 1
body 1
table 1
tbody 1
tr 1
td slno 1
td mname 2
td coll 3
***
In order to habdle completly dynamic elements we must fallow 3 steps
step1: First locate the static element
step2: From the static element move to immediate parent
step3: From the parent element locate the dynamic element
Sibling Function:
----------------
1.In order to move from 1 child to another child we go for sibling function
2.In siblings() we have 2 types
1.following-sibling
2.preceding-sibling
There are 2 types of terversing
1.Forward traversing: Navigating from parent to child element by using / and //
forward slash
2.Backward traversing: traversing from child to immediate parent by using /..
following-sibling:
-----------------
If an element is present on the rightside of the table and rightbelow the static
element in html tree structure is called as following sibling.
syntax: /following-sibling::tag
slno to collection
//td[text()='1']/following-sibling::td[text()='10cr']