1

this is not a logical question as such, but I really need to know what is the most effective way of writing xpath during selenium webdriver automation. Please explain a bit with few examples. Any help will be highly appreciated. Thanks

  • 1
    What do you mean by "effective"? Are you asking how to determine an element's XPath? Or how to write an XPath that might be less brittle? Or something else? – user246 Sep 11 '15 at 22:35
  • I don't want to close this outright, but I'm having a real tough time seeing an answerable question here. Could you elaborate more on what your actual problem is, and how we can go about solving it? – corsiKa Sep 11 '15 at 22:42
  • @above Yeah, what I meant was that I saw people using shorter versions of xpath, but sometimes my xpath that I locate through firepath contains a very long string with ul and li. Is there a way to make it small? – Uziii Sep 11 '15 at 23:18
  • It seems to me that you need to read some xpath tutorials to understand how the paths are constructed manually. Writing such tutorial here would be meaningless and that is why I'm voting to close this question. – Edu Sep 14 '15 at 6:39
  • Okay, yeah my intiuition was correct, this is simply too broad to objectively answer. I mean, we might be able to throw together a series of tips and tricks (in fact, check out DocOverflow or whatever they're going to call it) but there wouldn't be a right or wrong answer to this. That being said, if you find a particular xpath you're using unsatisfactory, you could always open a question about how to optimize that. – corsiKa Sep 14 '15 at 14:35
4

First: Ask your developers, if there could be unique identifiers in the tag parameters. Could be id, name or proprietal parameter. For example angular is using ng-* parameters for each object.

Study Xpath Tutorial (http://www.w3schools.com/xsl/xpath_intro.asp) to master your Xpath skills.

To create the particular xpath expression quickly, I use this steps:

  1. copypaste whole tag of the object you need to locate
  2. exchange < with //
  3. write [@ behind the tag
  4. delete the rest in exception of parameter identifying the object parameter="value"
  5. write ] on the end of xpath

You can combine xpath expressions to get specific path and distinguish between similar objects.

  • Try to use Xpath axes (http://www.w3schools.com/xsl/xpath_axes.asp) as /descendant::, /preceding-sibling::, ... etc. instead of full path. Developers often change structure of their code and structure of HTML can change a lot.
  • In case parameter contains some string, use contains(@parameter,"value") clausule.
  • In case parameter starts with some string and rest is rubbish, use starts-with(@parameter,"value") clausule.
  • use dot contains(.,"value") for text content of the web tag

For example for google code:

<form class="search-form" name="search" action="http://www.google.com/search" method="get">
    <input id="sbi" class="search-input" type="search" x-webkit-speech="" value="" name="q">

You can write xpath:

//form[@name="search"]/descendant::input[@type="search"]

or

//form[@name="search"]/descendant::input[starts-with(@class,"search")]

Code could be shorter (//input[@id="sbi"]) but in case id is changing during development, or there is more search dialogs/inputs on the page, it is practical to distinguish the position by parental element. Expression is more robust during development cycle and therefore you will not need to come back to it again.

Sometimes elements in the page are changed during developmnet, for example several <a> tags is replaced by <li> tags, but parameters will stay. Consider to replace a tag by * in your xpath expression.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.