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 ...
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 ...
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,...
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 ...
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 ...
7
votes
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 ...
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 ...
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']
...
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....
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:
...
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 "...
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 ...
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 ...
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 ...
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-...
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(...
4
votes
Correct CSS selector for selenium
If your edit class is unique on the page, then you can do .edit If you need to be a bit more specific, you could do more along the lines of .active.editing input.edit
The point is, theres no "right" ...
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 ...
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 ...
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-...
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 ...
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 ...
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 ...
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 ...
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:
...
3
votes
Accepted
How to generate good locators for elements in Web pages?
For all three parts of your question, I would suggest to apply one of the following approach (considering only web application, as mentioned in question itself):
First and the most naive approach is ...
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.
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 ...
2
votes
How to generate good locators for elements in Web pages?
There are a lot of guidelines that will help you in choosing good, reliable, reusable, maintainable selectors. Simply inspecting an element and copying its xpath is one of the worst approaches.
When ...
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.
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
css-selectors × 50selenium-webdriver × 37
xpath × 27
automated-testing × 16
java × 7
selenium2 × 3
c# × 3
protractor × 3
css × 3
jmeter × 2
browser-automation × 2
javascript × 2
page-factory × 2
magento × 2
test-management × 1
python × 1
test-automation-framework × 1
page-objects × 1
performance-testing × 1
eclipse × 1
chrome × 1
angular × 1
firefoxdriver × 1
coded-ui-tests × 1
specflow × 1