与findElement()一起使用的最有效的选择器是什么? [英] What is the most efficient selector to use with findElement()?

查看:99
本文介绍了与findElement()一起使用的最有效的选择器是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Selenium Web测试时,有几种方法可以识别WebElements。

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

根据我的经验,我使用了以下选择器:

In my experience, I have used the following selectors:


  • 类名 - By.className()

  • CSS选择器 - By.cssSelector()

  • ID - By.id()

  • 链接文字 - 按名称.linkText()

  • 名称 - By.name()

  • 标记名称 - By.tagName()

  • XPath - By.xpath()

  • 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()

显然,当只有一个选项可用于定位元素时,我们必须使用那个,但是当可以使用多个方法时(例如:下面的div),应该如何确定使用哪种方法?是否存在效率的选择器?是否有一些更持久

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>


推荐答案

仅限s& gs ......

Just for s&gs...

我为每个标识符方法计时,在五个单独的时间内找到div并平均查找元素所花费的时间。

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

  • 名称 :( 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

  • 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

在阅读@JeffC的回答后,我决定比较 By.cssSelector(),其中classname,tagname和id作为搜索词。同样,结果低于..

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 = 〜 804毫秒

    • 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
    • 从这一点来看,似乎你应该使用css选择器来做你能做的一切!

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

      这篇关于与findElement()一起使用的最有效的选择器是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆