Skip to main content
28 votes
Accepted

What makes a good selenium locator?

Good question, especially if people will read it and stop using XPath (I am not holding my breath). Selenium best practices mentions order of preference: id > name > css > xpath Mozilla explains why ...
Peter M. - stands for Monica's user avatar
20 votes

What makes a good selenium locator?

Choosing a good locator is very important to do carefully - it will define how reliable, readable, maintainable and durable your tests are going to be; how much dependent on the UI and design changes ...
alecxe's user avatar
  • 11.4k
9 votes

What makes a good selenium locator?

I think most answers are pretty good, but I would like to focus a bit on the higher level of these questions and not the details. What makes a good Selenium locator? Readability: Shorter is better,...
Niels van Reijmersdal's user avatar
8 votes

What makes a good selenium locator?

For my money it is CSS Locators. Uses ID and/or class if there is one and uses position otherwise. Plus it is super easy to get Chrome to give you a CSS selector and test it in the console tab of ...
kirbycope's user avatar
  • 4,090
8 votes
Accepted

How to find the first occurrence of a field that exists in multiple places

In XPath there are a couple of ways you could try this. Match by text (can be partial text too) (//label[contains(.,'Missing Applicant')])[1] Match by tag (if that helps isolate the label from ...
NotInventedHere's user avatar
8 votes
Accepted

syntax for css selector class attribute combination

Assume you want to take all the elements having class X but having no class Y. Then your code will look like: By.cssSelector(".x:not(.y)") For example for the below html: <div> <div ...
Alexey R.'s user avatar
  • 11.6k
6 votes

css locators - when should I use ">" vs. " "(space)

For ones who know xPath > would have pretty much the same meaning as / (slash) and " " (a whitespace) would mean the same as // (double slash). Hence > takes direct children of an element, but "...
Alexey R.'s user avatar
  • 11.6k
6 votes

What makes a good selenium locator?

What makes a good selenium selector? uses css unique robust short descriptive Given those quality attributes, in practice that translates into: Favor css over xpath for readability e.g. favor "form....
Michael Durrant's user avatar
6 votes
Accepted

Detecting complicated CSS selectors

What parameters would you consider to determine if a CSS selector is resistant to a change? What makes a reliable CSS locator? Some factors I would consider: To start with, the basics are: ...
Michael Durrant's user avatar
6 votes
Accepted

What is the hierarchy to use for finding elements - ID, class, name, xpath?

My main criteria is readability and maintainability. Speed has never been as issue for me. There's other parts of the test frameworks I use that have much more significant speed issues. The basic ...
Michael Durrant's user avatar
6 votes
Accepted

How to find element by class name or xPath?

can you please try the options below: By Xpath: button[@type='submit'] button[@class='btn btn-success'] button[@type='submit'][@class='btn btn-success'] By css selector: button[type='submit'] ...
Yu Zhang's user avatar
  • 9,942
6 votes

Are test-specific selectors a good idea?

I would say it is always a good idea from the standpoint of automated tests development efficiency. Unused attributes (you call them tags but they are rather the attributes) cannot be really ...
Alexey R.'s user avatar
  • 11.6k
6 votes

Are test-specific selectors a good idea?

tl;dr: class="menu-link" Putting test-specific code in production adds complexity to the production code and only helps QA. Instead, use it as an opportunity to make the production code better ...
Schwern's user avatar
  • 189
5 votes
Accepted

Unable to click button Selenium Webdriver 3

Try this xpath //*[contains(@class,'btn btn-primary btn-lg btn-block pink button') and contains(@tabindex,'4')] The xpath basically means : any element that has class attribute containing 'btn btn-...
BountyHunter's user avatar
5 votes

How to click a link when more than 1 exists?

When there are multiple instances and there is no other way to distinguish them* you can refer to the instance index, i.e. 'which one'. CSS a.instanceLink:nth-child(1) or a.instanceLink:nth-child(...
Michael Durrant's user avatar
5 votes
Accepted

Using Protractor to test AngularJS frontend- how do I get HTML DOM objects that are nested within other objects?

As an aside, although I will want to test the 'log in' capability too, is there a way to 'skip' this during testing, so that I can run the tests on all of the features of the app, without Protractor ...
alecxe's user avatar
  • 11.4k
4 votes

css locators - when should I use ">" vs. " "(space)

Possibly the largest difference, in automation terms, is how fixed and brittle the locators are. In the first example - the one with the >'s they effective mean that there must a div, with a div ...
Michael Durrant's user avatar
4 votes
Accepted

Are SelectById, SelectByClass and SelectByName just special cases of general SelectByTag?

Question 1 No, a tag name is what you find in brackets: <tagName></tagName> It is a shortcut for the CSS selector tagName Name, ID and class are all attributes of an element, they are ...
FDM's user avatar
  • 5,894
4 votes

Locating Inner Text with CSS or Xpath in Amazon

It's Pretty easy. Look at your XPath expression and the Syntax error: You have added unwanted bracket "()" in your XPath. Your XPath should be: //*[contains(text(), 'Hello,Nichole')] Using Page-...
Bharat Mane's user avatar
  • 6,785
4 votes
Accepted

Selecting second instance of identically named class using CSS Selectors

nth-child is used for list. In this case you need to identify a unique parent or sibling. I see that page-cards has multiple child statement. If you have only 2 statement elements and the order is ...
lauda's user avatar
  • 977
4 votes
Accepted

Want to test ebay site login without using Xpath

It looks live you've answered your own question driver.findElement(By.cssSelector("a[href='www.ebay.com']")) but the DOM says that element is <a href="https://www.ebay.com/" ..../> So ...
Doug Clark's user avatar
3 votes

Using Protractor to test AngularJS frontend- how do I get HTML DOM objects that are nested within other objects?

HTML id is not the same as HTML name. It's common to have fields where the name and the id are different: <input id="user_name" name="inputName"> If you change your code to find the fields to ...
Kate Paulk's user avatar
  • 31.5k
3 votes

How do I use a combination of attributes to select a button in Selenium?

The HTML you show has no ID (it's not the href that has a # btw, that's different). so findElement(By.Xpath(".//*[@id='step1']/button"))` will not work based on what you posted. You could use: ...
Michael Durrant's user avatar
3 votes

What makes a good selenium locator?

Explaining a bit more on how you can make your selector robust Bad selector If this is the selector you use .content > table > tbody > tr:nth-child(2) > td.cell > input#email And the ...
Joviano Dias's user avatar
3 votes

syntax for css selector class attribute combination

Why not just select: By.cssSelector('[class="X"]'); This will simply exclude the "Y" class objects without any NOT operators assuming there are NO "Z" class objects in the picture.
Vishal Aggarwal's user avatar
3 votes

locating element by id or css, which is faster?

Any WebDriver will use Json Wire Protocol to interact with the browser. The protocol defines 5 locator strategies: css selector, link text selector, partial link text selector, tag name, xpath. w3 ...
Moro's user avatar
  • 1,052
2 votes

How to find element by class name or xPath?

If the class name is unique in the page then you can use the class name. Keep in mind that the class name might not select the selector you need if there are multiple elements with the same class. ...
lauda's user avatar
  • 977
2 votes
Accepted

Can we programmatically update locators on UI Change in UI test automation?

I currently work on a solution for exactly this problem (well, more the Problem of not existing identifiers but unknown ID´s are as good as non existing). The idea is to use Seleniums getCurrentURL()...
Daniel's user avatar
  • 968
2 votes

How to locate "Login" button of Flipkart bootstrap login page? I tried in many ways, but still unable to locate element

Try using the following code for login functionality in Flipkart: driver.findElement(By.linkText("Login & Signup")).click(); driver.findElement(By.className("_2zrpKA")).sendKeys(" YOUR USER NAME"...
Kannathasan's user avatar
2 votes

css locators - when should I use ">" vs. " "(space)

'>' For immediate child element ' ' (space) for an child element on any sublevel. I prefer to use space as it covers both scenarios and less brittle in long time. From performance perspective as ...
Vishal Aggarwal's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible