Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Element Selectors - I thought xPath was slowest?

I ran some tests against a public website to see if I could find differences in the performance of a few different Selenium CSS selectors. I ran one hub with five nodes; mac/chrome/local, mac/safari/local, mac/ff/local, win7/ie9/localVM, and win8/ie10,localVM. The tests were all running in parallel, to try to simulate how I usually run them. I was surprised to see that xPath selectors did not turn out to be the devil that I expected. Maybe there is something funky about my tests? Anyone have any insight?

Here is the test code...

    int cycles = 500;
int yVal = 0;

getPage(“http://www.princeton.edu");

/* try an element that does not have an id*/
startStopwatch();
for (int i = 0; i < cycles; i++)
    yVal = driver.findElementByCssSelector("a[href='/main/news/events/']").getLocation().y;
print("By CSS: " + elapsedSeconds());

startStopwatch();
for (int i = 0; i < cycles; i++)
    yVal = driver.findElementByCssSelector("div[id='events'] a[href='/main/news/events/']").getLocation().y;
print("By CSS using id: " + elapsedSeconds());


startStopwatch();
for (int i = 0; i < cycles; i++)
    yVal = driver.findElementByXPath("//a[@href=\'/main/news/events/']").getLocation().y;
print("By xPath: " + elapsedSeconds());

/* try an element with an id */
//by id
startStopwatch();
for (int i = 0; i < cycles; i++)
    yVal = driver.findElementById("events").getLocation().y;
print("By Id: " + elapsedSeconds());

//by CSS
startStopwatch();
for (int i = 0; i < cycles; i++)
    yVal = driver.findElementByCssSelector("div[id='events']").getLocation().y;
print("By CSS: " + elapsedSeconds());

// an unnecessarily long xPath expression
startStopwatch();
for (int i = 0; i < cycles; i++)
    yVal = driver.findElementByXPath("//span[text()='News at Princeton']/ancestor::div[1]/following-sibling::div[1]").getLocation().y;
print("By longer xPath: " + elapsedSeconds());

// somewhat shorter xPath
startStopwatch();
for (int i = 0; i < cycles; i++)
    yVal = driver.findElementByXPath("//span[text()='Featured Events']/ancestor::div[1]").getLocation().y;
print("By shorter xPath: " + elapsedSeconds());

Here are results, showing xPath to hold its own, all times are in seconds for 500 iterations.

Selenium Selector Comparison Table

Safari was by far the most erratic performer, with times being strangely different for each test run.

princeton.edu is a pretty run-of-the-mill web page, with fairly easy selectors, but seems to suggest that xPath ain't that bad. I found very much the same thing when testing my work site.

Any thoughts on what I may be missing here??

like image 810
JackhammersForWeeks Avatar asked Mar 19 '14 22:03

JackhammersForWeeks


People also ask

Which locator is slowest in Selenium?

XPath Locator It is slowest among all locators. But it provides you reliable ways to locate web elements.

Why is XPath slowest?

XPath, on the other hand, is a generalized and complex expression language, that allows much more selections than CSS, and thus is harder to optimize.

Why XPath is slower than other locators?

Xpath allows bidirectional flow which means the traversal can be both ways from parent to child and child to parent as well. Css allows only one directional flow which means the traversal is from parent to child only. Xpath is slower in terms of performance and speed.

Which is faster CSS selector or XPath?

In terms of performance, css is better and faster, while xpath is on a slower side. An xpath can be of two types – absolute which starts from the root node and relative does not require to be started from the root. To traverse to the nth element, we have to mention [n] in the xpath, where n is the index number.


1 Answers

People seem to lazily assume Xpath is slow and should be avoided. When I interview people I cringe when they say they avoid Xpath because it is slow and brittle. The speed, as shown here, is no longer a concern, and xpath is only as brittle as the person who wrote it. In the right scenario Xpath is awesome and can actually improve performance as it allows you to perform in one command that may have taken several (e.g find element then iterate through sub elements can be performed in one xpath)

Oh, and dont get me started on people who think there is only one Xpath for an element and that it is found by right clicking in firebug

like image 90
Robbie Wareham Avatar answered Oct 02 '22 08:10

Robbie Wareham