techniques and tricks in using locators with selenium

29
Techniques and tricks in using locators -Sriram

Upload: sriram-subramanian

Post on 14-Apr-2017

96 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Techniques and tricks in using locators with selenium

Techniquesandtricksinusinglocators

-Sriram

Page 2: Techniques and tricks in using locators with selenium

Agenda

ØWhatlocatorsare?

ØDifferenttypesoflocators

ØWalkthroughoncss selectorsandxpath

ØDemo

Page 3: Techniques and tricks in using locators with selenium

Whatactuallyarelocators?

Page 4: Techniques and tricks in using locators with selenium

Differenttypesoflocators

Ø ID

ØName

Ø Linktext

Ø PartialLinktext

Ø TagName

Ø Classname

Ø Css

Ø Xpath

Page 5: Techniques and tricks in using locators with selenium

Whatiscss selector

Page 6: Techniques and tricks in using locators with selenium

Whatisxpath

Page 7: Techniques and tricks in using locators with selenium

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

Page 8: Techniques and tricks in using locators with selenium

IdentifyElementswithclassattribute

Page 9: Techniques and tricks in using locators with selenium

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’"));

Page 10: Techniques and tricks in using locators with selenium

IdentifyElementswithIDattribute

Page 11: Techniques and tricks in using locators with selenium

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’]"));

Page 12: Techniques and tricks in using locators with selenium

IdentifyElementsbyitstype

Page 13: Techniques and tricks in using locators with selenium

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"));

Page 14: Techniques and tricks in using locators with selenium

IdentifyElementswithparentandchildelement

types

Page 15: Techniques and tricks in using locators with selenium

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"));

Page 16: Techniques and tricks in using locators with selenium

IdentifyElementshavingaspecificattribute

Page 17: Techniques and tricks in using locators with selenium

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]"));

Page 18: Techniques and tricks in using locators with selenium

IdentifyElementsbywhatattributevaluestarts

with

Page 19: Techniques and tricks in using locators with selenium

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’)]"));

Page 20: Techniques and tricks in using locators with selenium

IdentifyElementsbywhatattributevalueends

with

Page 21: Techniques and tricks in using locators with selenium

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’)]"));

Page 22: Techniques and tricks in using locators with selenium

IdentifyElementsbywhatattributevaluecontains

Page 23: Techniques and tricks in using locators with selenium

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’]"));

Page 24: Techniques and tricks in using locators with selenium

Identify2nd childofaparentelement

Page 25: Techniques and tricks in using locators with selenium

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]"));

Page 26: Techniques and tricks in using locators with selenium
Page 27: Techniques and tricks in using locators with selenium
Page 28: Techniques and tricks in using locators with selenium

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

Page 29: Techniques and tricks in using locators with selenium

[email protected]

https://blogfromsriram.wordpress.com/