Why id
is the best way to find the element in Selenium WebDriver? Not XPath?
Asked
Active
Viewed 98 times
0

Lyubomyr Shaydariv
- 20,327
- 12
- 64
- 105

DJ developer
- 11
- 1
1 Answers
0
Under the hood it's just doing a javascript page search. Document.getElementById() is generally a constant time lookup (O(1)) because there can only be one element per an ID on page. See this answer for a more in-depth discussion. While on some browsers it's not a hash map lookup it's still likely to be much faster then xpath (see this answer).
See this perf test https://jsperf.com/getelementbyid-vs-queryselector/4 for a comparision.
-
thanks for the answer actually i am new in selenium webdriver and i got this question from a person and he said that xpath is slow in IE and thats why "ID" is best way to search. – DJ developer Jun 14 '15 at 19:00
-
could you please explain more ? – DJ developer Jun 14 '15 at 19:00