0

Why id is the best way to find the element in Selenium WebDriver? Not XPath?

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

1 Answers1

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.

Community
  • 1
  • 1
jpiasetz
  • 1,692
  • 4
  • 21
  • 35