0% found this document useful (0 votes)
82 views1 page

Locators Webdriver When Do You Use? When You Cannot Use?

1. The document lists different locator strategies in Selenium such as id, name, link text, tag name, class name, xpath, and css selector along with when to use each strategy and when not to use. 2. ID is preferable to xpath as a locator because it is unique, faster than traversing the DOM like xpath, and remains valid even if the DOM changes in the future unlike xpath which could become invalid.

Uploaded by

ARK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views1 page

Locators Webdriver When Do You Use? When You Cannot Use?

1. The document lists different locator strategies in Selenium such as id, name, link text, tag name, class name, xpath, and css selector along with when to use each strategy and when not to use. 2. ID is preferable to xpath as a locator because it is unique, faster than traversing the DOM like xpath, and remains valid even if the DOM changes in the future unlike xpath which could become invalid.

Uploaded by

ARK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

1.

Locators:

Locators WebDriver When do you use? When you cannot use?


Id id Always (Most preferred) Numbers
Name name If “id” does not exist Duplicate
Link linkText If it is a link Duplicate
Tag name tagName For collection of objects Duplicate
Class name className When class is unique w/o spaces White spaces, Duplicate
Xpath xpath If none of the above, works Xpath can change
Style sheet cssSelector This is the last option you have! CSS can change

Note: These are all methods of 'By' class in selenium and through findElement method (of
RemoteWebDriver class) or findElements, it will be called.
Referece: Go to https://seleniumhq.github.io/selenium/docs/api/java/ link to see all Java
Selenium classes and associated constructors and methods

2. Why ID is preferable than X-Path?

ID is the Preferred locator than X-Path because ID is Unique and is faster when compared
to X-Path (slower since it traverses through the DOM). When you find an web element using ID,
it would be easy to target the element successfully. Even though the DOM changes in the future,
ID remains same, whereas xpath may become invalid.

Note: If the ID contains numbers, it is not preferable to use ID since those numbers keeps
changing randomly.

You might also like