按id定位元素 [英] Locating an element by id
问题描述
以下位置技术有什么区别?
-
元素(by.id(id ));
-
元素(by.css(#id));
-
元素(by.xpath(// * [@ id ='id']));
-
browser.executeScript(return document.querySelector('#id'););
-
browser.executeScript(return document.getElementById('id'););
从性能角度来看,这将是以最快的方式定位元素。 你的问题很难回答,肯定会给一个单一的结论性答案。事实上,我试图将这个问题标记为太广泛,这得到了其他答案和评论的支持。 例如, c $ c> element(by.id(id)); 。通过Selenium源,大多数驱动程序只需接受您提供的任何ID,并将其传递到有线协议: 对于不支持wire协议的其他浏览器,例如HtmlUnit,你只需要具有类似: ,然后解析可用的DOM。 对于你的性能问题,任何人给你的任何东西都将是1)只是一个感觉,或2)纯BS! 要获得真实的回答(由实际数据支持),您可以从其他回答和评论中看到。 此外,无论您的网络应用/网页的结果如何,都不适用于其他网络应用/网页,由于用于构建该网站的框架的差异。 底线是:如果你关心性能测试,那么Selenium是错误的答案。 Selenium是一个功能测试库,优化为您提供最好的最终用户表示。 如果你的目标是让你的测试运行得更快,你的时间将更好地考虑你的测试结构:
public WebElement findElementById(String using){
if(getW3CStandardComplianceLevel()== 0){
return findElement(id,using);
} else {
return findElementByCssSelector(#+ cssEscape(using));
}
}
如你所知,协议在一个单独的二进制。
public List< WebElement> findElementsById(String id){
return findElementsByXPath(// * [@ id ='+ id +']);
}
考虑将您的元素移动到页面工厂模型,它会延迟加载所有元素。
但是我认为这是从你的初始问题的主题(有些可能建议ranty)。 >
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');");
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.
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.
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 + "']");
}
and then they parse the available DOM.
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:
- Wire protocol as implemented by different browser vendors, plus various optimizations in different versions.
- DOM engines as implemented by different browser vendors, plus various optimizations in different versions.
- JavaScript engines as implemented by different browser vendors, plus various optimizations in different versions.
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.
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:
- 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屋!