Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is the best and fastest way to find the element using webdriver? By.XPath or By.ID or anything else? And why? [closed]

Which is the best and fastest way to find the element using webdriver? By.XPath or By.ID or anything else? And why?

I have read at many places that XPath is the right candidate but just could not find the accurate reason for that.

like image 250
Tejas Avatar asked Aug 02 '12 12:08

Tejas


People also ask

Which is faster XPath or ID?

Technically speaking, By.ID() is the faster technique because at its root, the call goes down to document. getElementById(), which is optimized by most browsers. But, finding elements using XPath is better for locating elements having complex selectors, and is no doubt the most flexible selection strategy.

What is the fastest way to find an element on a page in Selenium?

ID locator in Selenium is the most preferred and fastest way to locate desired WebElements on the page. ID Selenium locators are unique for each element in the DOM. Since IDs are unique for each element on the page, it is considered the fastest and safest method to locate elements.

What is the fastest way to find an element on a page?

Xpath: Xpath is used to locate element on web page. It is also the fastest way for searching elements or objects, but it uses very complex selector in commands.

What is the best call for finding elements using XPath?

findElementsByXPath is the best call for finding multiple elements using XPath.


1 Answers

Finding elements by ID is usually going to be the fastest option, because at its root, it eventually calls down to document.getElementById(), which is optimized by many browsers.

Finding elements by XPath is useful for finding elements using very complex selectors, and is the most flexible selection strategy, but it has the potential to be very slow, particularly in IE. In IE 6, 7, or 8, finding by XPath can be an order of magnitude slower than doing the same in Firefox. IE provides no native XPath-over-HTML solution, so the project must use a JavaScript XPath implementation, and the JavaScript engine in legacy versions of IE really is that much slower.

If you have a need to find an element using a complex selector, I usually recommend using CSS Selectors, if possible. It's not quite as flexible as XPath, but will cover many of the same cases, without exhibiting the extreme performance penalty on IE that XPath can.

like image 51
JimEvans Avatar answered Oct 24 '22 10:10

JimEvans