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?
The different locators in Selenium are as follows:By CSS ID: find_element_by_id. By CSS class name: find_element_by_class_name. By name attribute: find_element_by_name. By DOM structure or xpath: find_element_by_xpath.
Type “css=input[type='submit']” (locator value) in Selenium IDE. Click on the Find Button. The “Sign in” button will be highlighted, verifying the locator value. Attribute: Used to create the CSS Selector.
Webpage elements can by foud by their id. That is one of the ways to select an element on a webpage with selenium. You must have the element id, which you can get with developer tools. You can also use id or css to select a webpage element.
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:
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:
But I think this is getting off topic (some might suggest "ranty") from your initial question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With