Locators
Locators
• driver.findElement(By.name(“email”));
linkText Locator:
• with the help of this linkText() method locator we
can only fetch the address of web element called
link.
• Links are always developed with a tag name called
a
• Here we pick the text value to fetch the address.
<a
href="https://www.facebook.com/recover/initiate/
?privacy_mutation_token=eyJ0eXBlIjowLCJjcmVhd
Glvbl90aW1lIjoxNjg4OTc4NjA1LCJjYWxsc2l0ZV9pZ
CI6MzgxMjI5MDc5NTc1OTQ2fQ%3D%3D&ars
=facebook_login"
waprocessedanchor="true">Forgotten
password?</a>
• driver.findElement(By.linkText(“Forgotten
password?”);
partialLinkText Locator:
• with the help of this partialLinkText() method
locator we can only fetch the address of web
element called link.
• Links are always developed with a tag name called a
• Whenever the text is very big and contains more
spaces, then we will use this method
• Here we pick the partial text value to fetch the
address of a web element.
• When ever text contains more spaces or large text
then we will use this locator.
<a
href="https://www.facebook.com/recover/initiate/
?privacy_mutation_token=eyJ0eXBlIjowLCJjcmVhd
Glvbl90aW1lIjoxNjg4OTc4NjA1LCJjYWxsc2l0ZV9pZ
CI6MzgxMjI5MDc5NTc1OTQ2fQ%3D%3D&ars
=facebook_login"
waprocessedanchor="true">Forgotten
password?</a>
• driver.findElement(By.partialLinkText(“password”));
driver.findElements(By.tagName(“a”));
package Locators;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
driver.close();
package Locators;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
<!DOCTYPE html>
<html>
<head>
<title>Sample Page</title>
</head>
<body>
<div class="container">
<h1>Sample Heading</h1>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<form>
<input type="text" id="username"
name="username" />
<input type="password" id="password"
name="password" />
<button type="submit">Login</button>
</form>
</div>
</body>
</html>
1. X path by Attribute:
Syntax:
//tagname[@Attribute name=’Attribute value’]
Drawbacks:
• Attributes are mandatory.
• It does not support text.
2. X path by Text:
Syntax:
//tagname[text()=’text value’]
Drawbacks:
• It will support only text.
• If the text is too big and contains more spaces
difficult to handle.
<a
href="https://www.facebook.com/recover/initiate/?p
rivacy_mutation_token=eyJ0eXBlIjowLCJjcmVhdGlvbl
90aW1lIjoxNjg5MTUxODcyLCJjYWxsc2l0ZV9pZCI6Mzg
xMjI5MDc5NTc1OTQ2fQ%3D%3D&ars=facebook
_login" waprocessedanchor="true">Forgotten
password?</a>
3. X path by contains:
It has two syntaxs
1. X path contains with respect to Attributes:
//tagname[contains(@Attributename,’Attribute
value’)]
2. X path contains with respect to text:
//tagname[contains(text(),’text value’)]
<a
href="https://www.facebook.com/recover/initiate/?p
rivacy_mutation_token=eyJ0eXBlIjowLCJjcmVhdGlvbl
90aW1lIjoxNjg5MTUxODcyLCJjYWxsc2l0ZV9pZCI6Mzg
xMjI5MDc5NTc1OTQ2fQ%3D%3D&ars=facebook
_login" waprocessedanchor="true">Forgotten
password?</a>
How to write this in script:
driver.findElement(By.xpath(“//input[contains(@wapro
cessedanchor,’true’)]”));
driver.findElement(By.xpath(“//input[contains(text(),Fo
rgotten’)]”));
Advantages:
• Easy to handle lengthy text and also text that
contains spaces.
• Supports both text and attributes.
Steps to be followed:
1. Identify the static element and write the x path
expression.
2. Identify the common parent (with the help of /..)
3. Write the tag name or xpath for dynamically
changing elements.
//span[contains(text(),'Samsung Galaxy M34 5G (Prism
Silver, 8GB, 128GB Storage')]/../../../..//span[@class='a-
price-whole']
Eg: //span[text()='itel
A60s']/../../../../../..//span[@class='a-price-whole']
4.
Static: The element which is fixed and does not
change.
Dynamic: The element which changes frequently,
Script:
package Locators;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
driver.get("https://www.amazon.in/");
driver.findElement(By.xpath("//input[@id='twotabsearchtextbox'
]")).sendKeys("samsung phone");
driver.findElement(By.xpath("//input[@id='nav-search-submit-
button']")).click();
WebElement value =
driver.findElement(By.xpath("//span[contains(text(),'Samsung
Galaxy M13 (Aqua Green, 4GB, 64GB
Storage)')]/../../../../../..//span[@class='a-price-
whole']"));
System.out.println(value.getText());
NOTE:
/ --->used for Traversing from parent to immediate
child
//---> used for Traversing from parent to any child
/.. --> used for Traversing from child to parent.
Position value of 6 is 4.
Eg: (//input[@type=’radio’])[4]