Xpath Web Server Performance

Is there a significant time difference required to calculate a long xpath than a short xpath?
Ex. Is there a performance difference between /div[@id = 'id1']/label[contains(text(), 'Hello')/../../descendant::input
and
//input

How about the difference between use By.id("id1")
and
By.Xpath("//*[@id='id1']")

+3
source share
1 answer

I'm glad you asked, I found the answers amazing.

  • A short xpath is faster than a long xpath, but not much
  • In Firefox, searching by name is faster than a long xpath, but dead heat with a short xpath (sometimes faster)
  • In Internet Explorer, By.name is significantly slower than xpath

, , re: IE xpath , , , .

, . Google

package com.PeterNewhook;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class FooTest {

public static void main(String[] args) {
    long start;
    long end;
    WebDriver driver;
    String longXpath = "/html/body/span[@id='main']/center/span[@id='body']/center/form/table/tbody/tr/td[2]/div[@class='ds']/input[@name='q']";
    String shortXpath = "//input[@name='q']";
    String elementId = "q";

    System.out.println("Using Firefox driver.");
    driver = new FirefoxDriver();
    driver.get("http://google.com");
    start = System.nanoTime();
    driver.findElement(By.xpath(longXpath));
    end = System.nanoTime()-start;
    System.out.println("The long XPath lookup took " + (double)end/1000000000.0 + " seconds.");
    start = System.nanoTime();
    driver.findElement(By.xpath(shortXpath));
    end = System.nanoTime() - start;
    System.out.println("The short XPath lookup took " + (double)end / 1000000000.0 + " seconds.");
    start = System.nanoTime();
    driver.findElement(By.name(elementId));
    end = System.nanoTime() - start;
    System.out.println("The By.name lookup took " + (double)end / 1000000000.0 + " seconds.");
    driver.close();

    System.out.println("\nUsing Internet Explorer driver.");        
    driver = new InternetExplorerDriver();
    driver.get("http://google.com");
    start = System.nanoTime();
    driver.findElement(By.xpath(longXpath));
    end = System.nanoTime()-start;
    System.out.println("The long XPath lookup took " + (double)end/1000000000.0 + " seconds.");
    start = System.nanoTime();
    driver.findElement(By.xpath(shortXpath));
    end = System.nanoTime() - start;
    System.out.println("The short XPath lookup took " + (double)end / 1000000000.0 + " seconds.");
    start = System.nanoTime();
    driver.findElement(By.name(elementId));
    end = System.nanoTime() - start;
    System.out.println("The By.name lookup took " + (double)end / 1000000000.0 + " seconds.");
    driver.close();
}
}

:

Firefox.
XPath 0.13667022 .
XPath 0,024628577 .
By.name 0.025209911 .

Internet Explorer.
XPath 0.196125248 .
XPath 0.164044262 .
By.name 1,005109964 .

+13

Source: https://habr.com/ru/post/1760542/


All Articles