IE 11 XPath lookup: Performance issue

Hello,
The AUT doesn’t have much ID attribute defined in the HTML. So I have looked up the elements in which I need to perform some actions based on some kind of uniqueness which is present in the vicinity of them, such as a header in a section or a text in a paragraph etc.

Since the AUT provides dynamic contents, I have generated the absolute XPath programmatically by traversing up to the root <html> tag. Some of the XPaths are like below:

  • /html[1]/body[1]/div[3]/div[1]/div[1]/div[2]/div[2]/div[2]/button[1]
  • /html[1]/body[1]/div[3]/div[1]/div[3]/div[1]/div[2]/div[1]/form[1]/span[1]/div[1]/div[1]/div[1]/input[1]

Afterward, I created the required TestObject programmatically, set the xpath TestObjectProperty into it and used the builtin or custom Keywords to perform actions on it.

Following is the performance matrix I created based on execution reports:

OS Browser Execution Time
Linux Chrome 00:04:30.322
Linux Firefox 00:04:28.065
Windows Chrome 00:04:47.536
Windows Edge 00:04:38.561
Windows Edge 00:04:54.141

For IE, the lookup of the elements using this absolute XPath is taking a huge amount of time, even if I set 30 seconds timeout for the lookup, sometimes it is failing. Unlike other browsers, the test is passing in IE intermittently.

I even have tried to set nativeEvents Desired Capabilities for IE to false (ref), hoping it could improve the performance, but no luck.

Is there any other way to overcome the problem?

Thanks in advance.
Regards.

1 Like

Fair warning: this answer is opinionated! :sunglasses:

If you can, avoid xpath. Reasoning: Browsers don’t use it to build, manage, operate on, web pages and their elements. (Yes, they supply some tools for devs and testers to play with, but that’s it). The problem then becomes as you’ve discovered: very little effort is spent optimizing the browser’s internals to handle xpath. And why would they? It has zero benefit for ordinary users (of whom there are hundreds of millions).

Hence, my opinion: avoid xpath. (Realize though, “avoid” != “don’t use” – use xpath only if you absolutely have to).

CSS, on the other hand, has the benefit of being part of the speed arms race. If you can convert to using CSS, you’ll never look back.

Caveat: Up to now, CSS does not do backward processing. That is, there are no “parent” or “ancestor” selectors.

Okay, that’s end of the opinionated piece.

====

You have the option of asking the devs to inject IDs and classes at crucial “waypoints” in the HTML. This would obviously help you shorten the paths required to target elements as you’d prefer.

Hope this gave you some ideas…

1 Like