techniques and tricks in using locators with selenium
TRANSCRIPT
Techniquesandtricksinusinglocators
-Sriram
Agenda
ØWhatlocatorsare?
ØDifferenttypesoflocators
ØWalkthroughoncss selectorsandxpath
ØDemo
Whatactuallyarelocators?
Differenttypesoflocators
Ø ID
ØName
Ø Linktext
Ø PartialLinktext
Ø TagName
Ø Classname
Ø Css
Ø Xpath
Whatiscss selector
Whatisxpath
HTML
BODY
DIV DIV DIV
DIV A A SPAN TABLE
TR
TD
TD
TD
TR
TD
TD
TD
class=banner id=banner
Attribute
href=“in.bookmyshow.com/f
avicon”
IMG
Attribute
href
IdentifyElementswithclassattribute
HTML
BODY
DIV DIV DIV
DIV A A SPAN TABLE
TR
TD
TD
TD
TR
TD
TD
TD
class=banner id=banner
Attribute
href=“in.bookmyshow.com/f
avicon”
IMG
Attribute
href
driver.findElement(By.cssSelector(”.banner"));driver.findElement(By.xpath(”//div[@class=‘banner’"));
IdentifyElementswithIDattribute
HTML
BODY
DIV DIV DIV
DIV A A SPAN TABLE
TR
TD
TD
TD
TR
TD
TD
TD
class=banner id=banner
Attribute
href=“in.bookmyshow.com/f
avicon”
IMG
Attribute
href
driver.findElement(By.cssSelector(”#banner"));driver.findElement(By.xpath(”//div[@id=‘banner’]"));
IdentifyElementsbyitstype
HTML
BODY
DIV DIV DIV
DIV A A SPAN TABLE
TR
TD
TD
TD
TR
TD
TD
TD
class=banner id=banner
Attribute
href=“in.bookmyshow.com/f
avicon”
IMG
Attribute
href
driver.findElements(By.cssSelector(”div"));driver.findElements(By.xpath(”//div"));
IdentifyElementswithparentandchildelement
types
HTML
BODY
DIV DIV DIV
DIV A A SPAN TABLE
TR
TD
TD
TD
TR
TD
TD
TD
class=banner id=banner
Attribute
href=“in.bookmyshow.com/f
avicon”
IMG
Attribute
href
driver.findElements(By.cssSelector(”tr >td"));driver.findElements(By.xpath(”//tr/td"));
IdentifyElementshavingaspecificattribute
HTML
BODY
DIV DIV DIV
DIV A A SPAN TABLE
TR
TD
TD
TD
TR
TD
TD
TD
class=banner id=banner
Attribute
href=“in.bookmyshow.com/f
avicon”
IMG
Attribute
href
driver.findElements(By.cssSelector(”[href]"));driver.findElements(By.xpath(”//*[@href]"));
IdentifyElementsbywhatattributevaluestarts
with
HTML
BODY
DIV DIV DIV
DIV A A SPAN
class=banner id=banner
Attribute
href=“in.bookmyshow.com/f
avicon”
IMG
Attribute
href
driver.findElement(By.cssSelector(”a[href^=‘in’]"));driver.findElement(By.xpath(”//a[starts-with(@href,’in’)]"));
IdentifyElementsbywhatattributevalueends
with
HTML
BODY
DIV DIV DIV
DIV A A SPAN
class=banner id=banner
Attribute
href=“in.bookmyshow.com/f
avicon”
IMG
Attribute
href
driver.findElement(By.cssSelector(”a[href$=‘favicon’]"));driver.findElement(By.xpath(”//a[ends-with(@href,‘favicon’)]"));
IdentifyElementsbywhatattributevaluecontains
HTML
BODY
DIV DIV DIV
DIV A A SPAN
class=banner id=banner
Attribute
href=“in.bookmyshow.com/f
avicon”
IMG
Attribute
href
driver.findElement(By.cssSelector(”a[href*=‘bookmyshow’]"));
driver.findElement(By.xpath(”//a[contains(@href,‘bookmyshow’]"));
Identify2nd childofaparentelement
HTML
BODY
DIV DIV DIV
DIV A A SPAN TABLE
TR
TD
TD
TD
TR
TD
TD
TD
class=banner id=banner
Attribute
href=“in.bookmyshow.com/f
avicon”
IMG
Attribute
href
driver.findElements(By.cssSelector(”td:nth-child(2)"));driver.findElements(By.xpath(”//td[2]"));
https://developers.google.com/web/tools/chrome-devtools/console/command-line-reference
http://www.w3schools.com/cssref/css_selectors.asp
http://www.w3schools.com/xml/xpath_intro.asp
https://blogfromsriram.wordpress.com/