Monthly Archives: February 2015

Search Page with xPath

There’s a neat trick in the Chrome Developer Tools which not too many people are aware of. Firstly, I’m just going to run through something you should already know. You can obtain the xPath value for an element in the page by:

  1. selecting the element in the Elements tab of the chrome developer tools; and
  2. right clicking on that Element and selecting copy XPath

copyXpath
That will copy the XPath value for that element into the clipboard. Really handy for things like the XPath selector in Selenium.

Sometimes, that XPath is not exactly the way you want to write it. For example, some XPath values will be more bullet-proof in terms of selecting an element because they may handle dynamically loaded elements better than the one which we extracted using the method above. (That’s perfect for the page in the state that it is in at that moment). But we may want to re-jig that XPath so that it will still find that element every day.

Here’s the neat trick. You can open the console and run XPath expressions against the page. The syntax is to simply using a lower case ‘x’ between the ‘$’ sign and the brackets: $x('xpath here')

For example:
consoleXPatHWithArrow
Dismissed!