Saturday, June 25, 2011

From Brittle XPath to CSS-selector

Use of xPath has taught me something valuable.... it is brittle!

So, with WebDriver, there are a few better ways to getting to certain elements or texts on a page:

Following our example site, on the this page we want to return a certain text.
"Etsy is more than a marketplace"

Here is a way to take your xPath and turn it into a more programmatic approach.

xpath on its own

//p[@class = 'quiet loud" and contains (text(), Etsy is more than a marketplace")]

By.cssSelector and Java or Groovy
findElement(By.cssSelector("p.quiet").getText().equals("Etsy is more than a marketplace")

One of the ways of finding the cssSelector in the page is with firebug, when you click on the element that you are trying to target a panel on the top of firebug will display the cssSelector.