長いxpathと短いxpathの評価にかかる時間に大きな違いはありますか?
元。との間にパフォーマンスの違いは
/div[@id = 'id1']/label[contains(text(), 'Hello')/../../descendant::input
ありますか
//input
By.id("id1")
との使用の違いはどうですか
By.Xpath("//*[@id='id1']")
長いxpathと短いxpathの評価にかかる時間に大きな違いはありますか?
元。との間にパフォーマンスの違いは
/div[@id = 'id1']/label[contains(text(), 'Hello')/../../descendant::input
ありますか
//input
By.id("id1")
との使用の違いはどうですか
By.Xpath("//*[@id='id1']")
あなたが尋ねてくれてうれしいです、私は答えが驚くべきことに気づきました。
これは、Simon Stewartが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秒かかりました。
InternetExplorerドライバーを使用します。
長いXPathルックアップには0.196125248秒かかりました。
短いXPathルックアップには0.164044262秒かかりました。
By.nameルックアップには1.005109964秒かかりました。