2010-08-19 5 views
3

짧은 xpath보다 긴 xpath를 평가하는 데 걸리는 시간에 큰 차이가 있습니까?
Ex. By.id("id1")
내가 너희를 물어 기쁘다
By.Xpath("//*[@id='id1']")Webdriver Xpath 성능

답변

13

를 사용하여 사이
/div[@id = 'id1']/label[contains(text(), 'Hello')/../../descendant::input

//input

무엇의 차이에 대한 사이의 성능 차이가 나는 대답이 놀라운 발견 .

  • 짧은 XPath는 긴 XPath는보다 빠른 아니라 파이어 폭스 이름으로 검색에 많은
  • 에 의해으로 긴 XPath는하지만, (때로는 빨리) 짧은 XPath를 가진 죽은 열 인터넷 익스플로러에
  • 보다 빠르다 .name을이다 XPath는보다 느린 많은
  • 이 사이먼 스튜어트가 다시 제공 된 지침의 얼굴에 날아 것으로 보인다

: IE의 XPath는 성능을, 그래서의 곡물로 가져 것 소금,하지만 아래 코드에서는 꽤 일관성이 있습니다.

나는 이것을 보여주는 간단한 테스트를 작성했다.

파이어 폭스 드라이버를 사용 : 그것은이 출력을 제공하는 구글

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(); 
} 
} 

에있는 검색 창을 찾습니다.
긴 XPath 조회에는 0.13667022 초가 걸렸습니다.
짧은 XPath 조회에는 0.024628577 초가 소요되었습니다.
By.name 조회에 0.025209911 초가 소요되었습니다.

Internet Explorer 드라이버를 사용 중입니다.
긴 XPath 조회에는 0.196125248 초가 걸렸습니다.
짧은 XPath 조회에는 0.164044262 초가 걸렸습니다.
By.name 조회에 1.005109964 초가 걸렸습니다.