When working with Selenium web testing, there are a few ways to identify WebElements.
In my experience, I have used the following selectors:
I'd prioritise selectors like this:
By.id()By.name()By.cssSelector()By.xpath()By.tagName()By.linkText()However, uniq IDs and Names are not always exist. Also you can use CSS Selectors to locate by ID #element_id or by Name [name=element_name] or by ClassName .element_class, so might be good idea to use CSS Selectors instead ID, Name and ClassName. Css is faster than xPath, so it's better to use it where possible. xPath is good for difficult element locators which CSS Selectors can't find. I also wouldn't use Tag Name and Link Text as you can write the same with xPath(For Link Text //a[text()='link_text'], Tag Name //div) or CSS Selectors(For Tag Name div ).