【问题标题】:Webdriver Xpath PerformanceWebdriver Xpath 性能
【发布时间】:2010-08-19 13:33:20
【问题描述】:

评估长 xpath 与评估短 xpath 所需的时间有很大不同吗?
前任。 有没有性能差异
/div[@id = 'id1']/label[contains(text(), 'Hello')/../../descendant::input

//input

使用和使用的区别是什么 By.id("id1")

By.Xpath("//*[@id='id1']")

【问题讨论】:

    标签: webdriver


    【解决方案1】:

    很高兴你问到,我发现答案令人惊讶。

    • 短 xpath 比长 xpath 快,但相差不大
    • 在 Firefox 上,按名称搜索比长 xpath 快,但短 xpath 死热(有时更快)
    • 在 Internet Explorer 上,By.name 比 xpath 慢

    这似乎与 Simon Stewart 给出的指导背道而驰: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 秒。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-27
      • 2018-01-21
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多