0% found this document useful (0 votes)
133 views

2.2 Day12-Locators PDF

This document discusses various locators that can be used to identify elements in a web page using Selenium WebDriver. It describes locating elements by ID, name, link text, CSS selector, and XPath. Examples are provided for each locator type to demonstrate how to locate elements uniquely based on attributes. The document also covers different XPath techniques like absolute vs relative XPath, using contains and starts-with methods, and combining locators with logical operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views

2.2 Day12-Locators PDF

This document discusses various locators that can be used to identify elements in a web page using Selenium WebDriver. It describes locating elements by ID, name, link text, CSS selector, and XPath. Examples are provided for each locator type to demonstrate how to locate elements uniquely based on attributes. The document also covers different XPath techniques like absolute vs relative XPath, using contains and starts-with methods, and combining locators with logical operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Selenium WebDriver

Agenda
➢ Locators in WebDriver
Locators
• We can identify various elements on the web using Locators.
• Locators are addresses that identify a web element uniquely within the page.
Locators

Linktext
id name CSS Selector XPath
PartialLinktext

• Tag and ID • Absolute XPath


• Tag and Class • Relative XPath
• Tag and attribute
• Tag, class and attribute
Locating by ID
driver.findElement(By.id("email")).sendKeys(“[email protected]");
Locating by the name
driver.findElement(By.name("userName")).sendKeys("mercury");
Locating by the Link Text
driver.findElement(By.linkText("REGISTER")).click();
CSS Selector - Cascading Style Sheets
➢Tag and ID

➢Tag and Class

➢Tag and attribute

➢Tag, class and attribute


CSS Selector – Tag and ID
driver.findElement(By.cssSelector("input#email")).sendkeys("[email protected]");
CSS Selector – Tag and Class
driver.findElement(By.cssSelector("input.inputtext")).sendkeys("[email protected]");
Multiple elements have the same tag and class name
➢ when multiple elements have the same HTML tag and name, only the first element in source code will
be recognized.

driver.findElement(By.cssSelector("input.inputtext")).sendkeys("[email protected]");
CSS Selector – Tag and Attribute
driver.findElement(By.cssSelector("input[name=lastName]")).sendkeys("xxxxx");
CSS Selector - Tag, class and attribute
driver.findElement (By.cssSelector("input.inputtext[tabindex=2]").sendkeys("xxxx");
XPath
• XPath is for locating the elements or nodes in XML documents

• XML and HTML has similar syntax (HTML is a subset of XML)

• Hence XPath can be used to locate elements in HTML pages (web pages).
Capture XPath
Types of XPath
➢There are two types of XPath:

1. Absolute XPath .

2. Relative XPath .
Absolute XPath
➢ It
is the direct way to find the element, but the disadvantage of the absolute XPath is that if there are any changes
made in the path of the element then that XPath gets failed.

➢ The key characteristic of


XPath is that it begins with the single forward slash(/) ,which means you can select the
element from the root node.
Relative XPath
➢For Relative Xpath the path starts from the middle of the HTML DOM structure. Its start
with the double forward slash (//), which means it can search the element anywhere at the
webpage.
Syntax for XPath
➢XPath contains the path of the element situated at the web page.

➢Standard syntax for creating XPath is.


Xpath=//tagname[@attribute='value‘]

// : Select current node.


Tagname: Tagname of the particular node.
@: Select attribute.
Attribute: Attribute name of the node.
Value: Value of the attribute.
XPath with Single attribute
// tagname[@attribute-name=’value1’]

Examples:

// a [@href=’http://www.google.com’]
//input[@id=’name’]
//input[@name=’username’]
//img[@alt=’sometext’]
XPath with Multiple attributes
//tagname[@attribute1=’value1’][attribute2=’value2’]

Examples:
//a[@id=’id1’][@name=’namevalue1’]
//img[@src=’’][@href=’’]
XPath with text() method
• Xpath=//td[text()='UserID']
XPath with Contains() method
//tagname[contains(@attribute,’value1’)]

//input[contains(@id,’’)]

//input[contains(@name,’’)]

//a[contains(@href,’’)]

//img[contains(@src,’’)]

//div[contains(@id,’’)]
XPath with starts-with() method
//tagname[starts-with(@attribute-name,’’)]

Examples:

//id[starts-with(@id,’’)]

//a[starts-with(@href=’’)]

//img[starts-with(@src=’’)]

//div[starts-with(@id=’’)]

//input[starts-with(@id=’’)]

//button[starts-with(@id,’’)]
Using OR, AND with XPath
//*[@type='submit' OR @name='btnReset’] //input[@type='submit' and @name='btnLogin']
Following
➢ Selects all elements in the document of the current node

//*[@type='text']//following::input[1]
//*[@type='text']//following::input
Preceding
➢ Select all nodes that come before the current node
//*[@type='submit']//preceding::input
Summary of Locators
Locators Description Example
By.className finds elements based on the value of the "class" attribute findElement(By.className("someClassName"))

By.cssSelector finds elements based on the driver's underlying CSS findElement(By.cssSelector("input#email"))


Selector engine
By.id locates elements by the value of their "id" attribute findElement(By.id("someId"))

By.linkText finds a link element by the exact text it displays findElement(By.linkText("REGISTRATION"))

By.name locates elements by the value of the "name" attribute findElement(By.name("someName"))

By.partialLinkText locates elements that contain the given link text findElement(By.partialLinkText("REG"))

By.tagName locates elements by their tag name findElement(By.tagName("div"))


By.xpath locates elements via XPath findElement(By.xpath("//html/body/div/table/tbody/tr/td[2]/table
/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[2]/td[3]/
form/table/tbody/tr[5]"))

You might also like