People who code: we want your input. Take the Survey

Hot answers tagged

26

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 Simon ...


20

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 ...


9

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 ...


8

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 ...


6

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.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 ...


4

If you are asking about this post button - try below code to click on post button. for this- WebElement postBtn = driver.findElement(By.xpath("//button[contains(.,'Post')]")); postBtn.click(); OR WebElement postBtn = driver.findElement(By.xpath("//button[@class='_1mf7 _4jy0 _4jy3 _4jy1 _51sy selected _42ft']")); postBtn.click();


3

// tagNameString is equal to "storytext" driver.findElements(By.tagName(tagNameString)); 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: WebElement ...


3

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 input is moved out of table or even moved a cell, the selector breaks Goodish selector But if the selector was .content input#email Then it would work even if ...


3

CSS selectors can also match strings that start with, end with, or contain a certain substring. For example input[id^="E_DOS_TITEL"] 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 ...


2

The most solid approach would be to render it, and see what's the style; and that's easier than it sounds :) There's a Javascript method getComputedStyle which should do the heavy lifting for you - it returns literally what its name says, the element's styling after all css is applied. A sample js code: var elem = document.getElementById("myID"); var ...


2

String option = "esporte interativo"; String locator = "//ul[@id='facebar_typeahead_view_list']" + "/li[contains(normalize-space(.), '" + option +"')]"; driver.findElement(By.xpath(locator)).click();


2

Have you tried the IE developer tools (F12)?


2

It is not an image (IMG) or a link (A) tag, it is a INPUT tag: By.xpath("//input[@src='/TelsimGlobal/Menu/image3.gif']")


2

"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: //div[@class='side_categories']/ul/li/ul/li/a In test code, it should look like something this: @FindBy(xpath= "//div[@class='side_categories']/ul/li/ul/li/a") List<WebElement> links; Then to get the link and the item ...


1

By.XPath("//*[@data-qa='homephone-update' and @class='ng-invalid']"); Try this


1

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 - //a[@id='nav-link-yourAccount']/span[contains(.,'Nichole')] e.g. @FindBy (how=How.XPATH, using= "//a[@id='nav-link-yourAccount']...


1

Ideally, the most preferred locator to recognize a web-element in Selenium WebDriver is ID. Reasons? 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-...


1

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 ...


1

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. ...


1

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 ...


1

Inspect the element with below xpath locator : By.xpath("//*[contains(@class, '_1mf7 _4jy0 _4jy3 _4jy1 _51sy selected _42ft') and @type='submit']"); Hope it will help resolve your problem.


1

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")); ...


1

form.form-horizontal > div:nth-child(3) > div > input I suggest spending 20 minutes here


1

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 ...


1

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 ...


1

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 ...


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