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 IDs
Saucelabs explains why CSS locators are preferred over XPath
slideshare compares locators (slide 23: CSS vs XPath)
CSS vs XPath - with benchmark
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 they are gonna be. Remember: maintaining end-to-end tests is, generally speaking, difficult and expensive (good read on the subject).
Here is a set of things ...
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, preferable with a clear unique name/id which describes this unique element on the page. Feel free to change the code like classes/names/id's to make the ...
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 DevTools via document.querySelector("yourCssSelectorHere") or doing a search on the Elements tab and pasting it in.
Most experienced Selenium users recommend CSS ...
What makes a good selenium selector?
Given those quality attributes, in practice that translates into:
Favor css over xpath for readability
e.g. favor "form.new_user input.age" over "//form[@class='new_user']/input[@class='age']"
Favor id's on the last element of a selector
e.g. form.new_user input#last_name
If you are asking about this post button - try below code to click on post button.
WebElement postBtn = driver.findElement(By.xpath("//button[contains(.,'Post')]"));
WebElement postBtn = driver.findElement(By.xpath("//button[@class='_1mf7 _4jy0 _4jy3 _4jy1 _51sy selected _42ft']"));
// tagNameString is equal to "storytext"
What you are actually asking Selenium to do is to find all "storytext" elements. Selenium will be looking for <storytext>...</storytext> elements which, I am pretty sure, you have none.
Instead, you intended to use the "by id" locator:
Explaining a bit more on how you can make your selector robust
If this is the selector you use
.content > table > tbody > tr:nth-child(2) > td.cell > input#email
And the input is moved out of table or even moved a cell, the selector breaks
But if the selector was
Then it would work even if ...
CSS selectors can also match strings that start with, end with, or contain a certain substring. For example
will match an input element whose id attribute starts with the text E_DOS_TITEL. The ends with comparison operator is $=, and contains is *=.
The example provided above should work as long as it's the only input element on ...
The most solid approach would be to render it, and see what's the style; and that's easier than it sounds :)
A sample js code:
var elem = document.getElementById("myID");
"Books" is a single list item, which contains a new list with the categories. So you need to go deeper into that list like this:
In test code, it should look like something this:
Then to get the link and the item ...
The way you are using cssSelector to find an element using some text, It doesn't allow in cssSelector. There is no such method in CSS selector to locate an element using text so for that you have to use xpath locator as below -
@FindBy (how=How.XPATH, using= "//a[@id='nav-link-yourAccount']...
Ideally, the most preferred locator to recognize a web-element in Selenium WebDriver is ID.
It is short.
It is fastest compared to other locators since in the background all
it needs to do is pick the element matching the mentioned ID.
It’s safest, as even if the location of this element changes or
worse, even if it’s type changes, your test-...
If you want to automate this, I'm aware of two possibilities:
As suggested by Bobby231, you could use a visual testing tool such as Depicted (open source) or Applitools (closed source) to check the rendered result of the website against a golden master.
There’s recheck-web (open source; disclaimer: I work for retest, the company behind the project). recheck ...
Cribbing from an answer from https://stackoverflow.com/questions/7754469/export-css-of-dom-elements:
Not a direct answer, but with Chrome Developer Tools, you can click
inside Styles or Computed Styles, hit Ctrl+A and then Ctrl+C to copy
all the styles in those given areas. It's not perfect in the Style tab
because it picks up some extra stuff. ...
You will need to write some custom code. There are multiple ways to do this depending on your end goal. There is a code only approach like what YuZhang suggests in utilizing a python library for pulling the info out of the page and checking it.
You could also pull the specifics with Selenium in the middle of a test with some tweaking. You pull the full ...
For those who are looking to do Selenium css text selections this script might be of some use
Trick is to select parent of element of one that you are looking for and then search for child that has the text.
public static IWebElement FindByText(this IWebDriver driver, string text)
var list = driver.FindElement(By.CssSelector("#RiskAddressList"));
Assuming this is unique on the page, you should be able to identify and click the same element using CSS = '[data-testid="media-attachment-selector"]'. (You could also pre-fix the element with a to indicate it's a link but either should work.)
Typically development teams include data-testids for testability / automated testing. It might be worth following ...
I would say No. You dont need to keep these files versioned. Ie TestImage1.png will never be updated or changed, as such I would suggest you store them alongside your test results. At this point you wouldn't be using Git for version control, simply a file store.
However, if you wish to maintain the images as "data" (ie used as a means of comparision to ...
Data is always better if it is stored in a central location where it is accessible to all the team members.
It may happen that your team is distributes in different geographical location. In that case if your keep data in your local system, then every time you will have to send the data to your team member viz mail or chat or any other file/data transfer ...