Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

java - What is the most efficient selector to use with findElement()?

When working with Selenium web testing, there are a few ways to identify WebElements.

In my experience, I have used the following selectors:

  • Class Name - By.className()
  • CSS Selector - By.cssSelector()
  • ID - By.id()
  • Link Text - By.linkText()
  • Name - By.name()
  • Tag Name - By.tagName()
  • XPath - By.xpath()

Obviously, when only one option is usable to locate an element, we must use that one, but when multiple methods may be used (ex: the div below), how should it be determined which method to use? Are there selectors that are more efficient than others? Are there some that are more durable?

<div class="class" id="id" name="name">Here's a div</div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just for s&gs...

I timed each of the identifier methods finding the div above five separate times and averaged the time taken to find the element.

WebDriver driver = new FirefoxDriver();
driver.get("file://<Path>/div.html");
long starttime = System.currentTimeMillis();
//driver.findElement(By.className("class"));
//driver.findElement(By.cssSelector("html body div"));
//driver.findElement(By.id("id"));
//driver.findElement(By.name("name"));
//driver.findElement(By.tagName("div"));
//driver.findElement(By.xpath("/html/body/div"));
long stoptime = System.currentTimeMillis();
System.out.println(stoptime-starttime + " milliseconds");
driver.quit();

They are sorted below by average run time..

  • CssSelector: (796ms + 430ms + 258ms + 408ms + 694ms) / 5 = ~517.2ms
  • ClassName: (670ms + 453ms + 812ms + 415ms + 474ms) / 5 = ~564.8ms
  • Name: (342ms + 901ms + 542ms + 847ms + 393ms) / 5 = ~605ms
  • ID: (888ms + 700ms + 431ms + 550ms + 501ms) / 5 = ~614ms
  • Xpath: (835ms + 770ms + 415ms + 491ms + 852ms) / 5 = ~672.6ms
  • TagName: (998ms + 832ms + 1278ms + 227ms + 648ms) / 5 = ~796.6ms

After reading @JeffC 's answer I decided to compare By.cssSelector() with classname, tagname, and id as the search terms. Again, results are below..

WebDriver driver = new FirefoxDriver();
driver.get("file://<Path>/div.html");
long starttime = System.currentTimeMillis();
//driver.findElement(By.cssSelector(".class"));
//driver.findElement(By.className("class"));
//driver.findElement(By.cssSelector("#id"));
//driver.findElement(By.id("id"));
//driver.findElement(By.cssSelector("div"));
//driver.findElement(By.tagName("div"));
long stoptime = System.currentTimeMillis();
System.out.println(stoptime-starttime + " milliseconds");
driver.quit();
  • By.cssSelector(".class"): (327ms + 165ms + 166ms + 282ms + 55ms) / 5 = ~199ms
  • By.className("class"): (338ms + 801ms + 529ms + 804ms + 281ms) / 5 = ~550ms
  • By.cssSelector("#id"): (58ms + 818ms + 261ms + 51ms + 72ms) / 5 = ~252ms
  • By.id("id") - (820ms + 543ms + 112ms + 434ms + 738ms) / 5 = ~529ms
  • By.cssSelector("div"): (594ms + 845ms + 455ms + 369ms + 173ms) / 5 = ~487ms
  • By.tagName("div"): (825ms + 843ms + 715ms + 629ms + 1008ms) / 5 = ~804ms

From this, it seems like you should use css selectors for just about everything you can!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...