Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
583 views
by Expert (572 points)
retagged by

What is the ideal way to identify locator in Selenium WebDriver?

Also, I would like to know,

  • How can one find a good locator expression?
  • What tools can generate expressions matching a certain element automatically?
  • How to troubleshoot such issues when the element does not match expression?

3 Answers

0 like 0 dislike
by The go-to Tester (142 points)
I would go for ID or name.

If that does not work, I would go for className. In case of a link, I would go for LinkedText or PartialLinkedText.

If none of the above worked, I would go for relative xpath.

Worse case, I would go for absolute xpath or CSS selector or byTagName.
0 like 0 dislike
by
1. ID

2. Relative Xpath  but I always create "Customize Xpath".  Never use Absolute Xpath

3. Name

4. Css selector

Best way to create the "Xpath" by suing chrome browser & after Inspect the WebElement, hit the Ctrl+F, then create your Customize Xpath.
0 like 0 dislike
by

If the DOM code has ID's that's where I would start.
If you need to add something to your code to be able to access the elements, I would say to have some custom classes that can be identified with selenium (doesn't work with coypu though). This will only help if you manage to get a custom class that will work across your entire program and with the help of the developers (they usually forget to add this class because it is "for the sole purpose of automation so not worth the effort").

When everything else fails Xpath is your solution.

You can grab xpath expresions with a firefox extension called FirePath, you might also find xpath finder useful.

You can always use the inspector from the browser itself to find any of these (Id, Css, Xpath).


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...