通过 id 定位元素 [英] Locating an element by id
问题描述
以下定位技术有什么区别?
What is the difference between the following location techniques?
element(by.id("id"));
element(by.css("#id"));
element(by.xpath("///*[@id='id']"));
browser.executeScript("return document.querySelector('#id');");
browser.executeScript("return document.getElementById('id');");
而且,从性能的角度来说,通过 id 定位元素的最快方法是什么?
And, from the performance perspective, which would be the fastest way to locate an element by id?
推荐答案
你的问题很难回答,当然要给出一个决定性的答案.事实上,我很想将这个问题标记为过于宽泛",其他答案和评论也支持这一点.
Your question is very difficult to answer, certainly to give a single conclusive answer. In fact, I am tempted to flag this question as "too broad", which is supported by the other answers and comments.
以您的 element(by.id("id"));
为例.查看 Selenium 源代码,大多数 驱动程序只需获取您提供的任何 id,并将其传递给有线协议:
Take, for example, just your element(by.id("id"));
. Looking through the Selenium source, most drivers just take whatever id you give, and pass it off to the wire protocol:
public WebElement findElementById(String using) {
if (getW3CStandardComplianceLevel() == 0) {
return findElement("id", using);
} else {
return findElementByCssSelector("#" + cssEscape(using));
}
}
如您所知,每个浏览器供应商都在单独的二进制文件中实现自己的有线协议.随意深入了解代码,为自己挖一个更深的洞.
As you know, each browser vendor implements their own wire protocol in a separate binary. Feel free to go further into the code, to dig a deeper hole for your self.
对于其他不支持有线协议的浏览器,例如 HtmlUnit,你只需要像这样:
For other browsers that do not support the wire protocol, for example HtmlUnit, you just have something like:
public List<WebElement> findElementsById(String id) {
return findElementsByXPath("//*[@id='" + id + "']");
}
然后他们解析可用的 DOM.
and then they parse the available DOM.
至于你的表现问题,任何人给你的任何东西都将是 1) 只是一种感觉,或者 2) 纯粹的 BS!您已经可以从您收到的其他答案和评论中看到.
As for your performance question, anything that anyone gives you will be 1) just a feeling, or 2) pure BS! Which you can already see from the other answers and comments you are getting.
要获得真实的答案(有实际数据支持),需要考虑的变量太多:
To get a real answer (supported by actual data), there are just too many variables to consider:
- 不同浏览器供应商实施的有线协议,以及不同版本的各种优化.
- 由不同浏览器供应商实现的 DOM 引擎,以及不同版本的各种优化.
- 由不同浏览器供应商实现的 JavaScript 引擎,以及不同版本的各种优化.
此外,由于用于构建该网站的框架存在差异,无论您为您的网络应用程序/网页获得的任何结果都最可能不适用于不同的网络应用程序/网页.
Also, whatever results you get for your web app / web page will most like not apply to a different web app / web page, due to differences in the framework used to build that site.
底线是:如果您关心性能测试,那么 Selenium 是错误的答案.Selenium 是一个功能测试库,经过优化可以为您提供最佳的最终用户表示.性能是一个遥远的事后想法.
Bottom line is: If you are concerned about performance testing, then Selenium is the wrong answer. Selenium is a functional test library, optimized to give you the best end-user representation. Performance is a distant afterthought.
如果您的目标是让测试运行得更快,那么您最好将时间花在查看测试结构上:
If your goal is to get your tests to run faster, your time will be better spent looking at your test structure:
- 您打开/关闭浏览器的频率.这通常是测试中最耗时的活动.
- 您多久刷新一次元素缓存,您需要多久刷新一次?考虑将您的元素移动到 Page Factory 模型,它会为您延迟加载所有元素.
- 当然还有最大的加速因素:在多台机器上并行运行测试.
- How frequently do you open/close the browser. This is often the most time consuming activity in a test.
- How often do you refresh your element cache, how often do you need to? Consider moving your elements to Page Factory model, which lazy-loads all elements for you.
- And of course the biggest speedup factor: running your tests in parallel on multiple machines.
但我认为这与您最初的问题无关(有些人可能会建议愤怒").
But I think this is getting off topic (some might suggest "ranty") from your initial question.
这篇关于通过 id 定位元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!