Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Locators in Selenium
Presented By
Dwarika Dhish Mishra
Email : dwarika1987@gmail.com
Mob: 9999978609
Blog: http://abodeqa....
What are locators?
Locators are a way to tell selenium which specific element
we want it to act on
Locators used:
• Id
• N...
Finding Element By Id
• Id attribute is most preferred way to locate element on a
webpage, since as per W3C standard every...
Finding Element By Id
So to perform action on Username and
Password in Selenium WebDriver we need
write this line of code
...
Finding Element by Name
<form name="loginForm">
<label for="username">UserName: </label>
<input type="text”name="username"...
Finding Element by Name
Since in some situations we don’t have Id
attribute provided in HTML so in that case we
use some o...
Find Element by Xpath
• Using Direct Xpath
Like /html/body/Element
• Using nth of type
• Using attribute
Like //input*@nam...
Continue…
• Pattern matching using starts-with() function
$x("//input[starts-with(@id,‘us')]")
• Pattern matching using en...
Continue…
• Axis method
– following-sibling::HTML tag
Like $x("//input[substring(@id,3)='ername']/followingsibling::input[...
Coming session topic
-CSS Locator
- Launching Chrome Browser /Firefox Browser/IE Browser
- Testng framework and its annota...
Thank you !!
Upcoming SlideShare
Loading in …5
×

Locators in selenium - BNT 09

1,613 views

Published on

Selenium Series

Published in: Education, Technology, Business
  • Be the first to comment

Locators in selenium - BNT 09

  1. 1. Locators in Selenium Presented By Dwarika Dhish Mishra Email : dwarika1987@gmail.com Mob: 9999978609 Blog: http://abodeqa.wordpress.com
  2. 2. What are locators? Locators are a way to tell selenium which specific element we want it to act on Locators used: • Id • Name • Class • Tag • linkText • Xpath • CSSLocator
  3. 3. Finding Element By Id • Id attribute is most preferred way to locate element on a webpage, since as per W3C standard every element should have a unique Id so that It could be indentified uniquely <form name="loginForm"> <label for="username">UserName: </label> <input type="text" id="username" /><br/> <label for="password">Password: </label> <input type="password" id="password" /><br/> <input name="login" type="submit" value="Login" /> </form>
  4. 4. Finding Element By Id So to perform action on Username and Password in Selenium WebDriver we need write this line of code WebElement username = driver.findElement(By.Id(“username”)); WebElement username = driver.findElement(By.Id(“password”));
  5. 5. Finding Element by Name <form name="loginForm"> <label for="username">UserName: </label> <input type="text”name="username" /><br/> <label for="password">Password: </label> <input type="password" name="password" /><br/> <input name="login" type="submit" value="Login" /> </form>
  6. 6. Finding Element by Name Since in some situations we don’t have Id attribute provided in HTML so in that case we use some other attribute like name and Class and Tagname So Line of code that we need to use WebElement username = driver.findElement(By.Name(“username”)); WebElement username = driver.findElement(By.Name(“password”));
  7. 7. Find Element by Xpath • Using Direct Xpath Like /html/body/Element • Using nth of type • Using attribute Like //input*@name=‘username’+ • Using partial match by using contain() function //input[Contains(@name,’user’)+
  8. 8. Continue… • Pattern matching using starts-with() function $x("//input[starts-with(@id,‘us')]") • Pattern matching using ends-with() function $x("//input[ends-with(@id,‘name')]") • Finding element using substring() function when we are dealing with some dynamic element $x("//input[substring(@id,3)='ername']")
  9. 9. Continue… • Axis method – following-sibling::HTML tag Like $x("//input[substring(@id,3)='ername']/followingsibling::input[@id='password']") Or $x("//input[substring(@id,3,5)='ernam']/followingsibling::input[@id='password']") – preceding-sibling::HTML tag – using ‘..’ to navigate to parent $x("//input[substring(@id,3)='ername']/..")
  10. 10. Coming session topic -CSS Locator - Launching Chrome Browser /Firefox Browser/IE Browser - Testng framework and its annotation used in Selenium
  11. 11. Thank you !!

×