Between By.XPath and By.ID, which is the better for locating elements?

0 votes
I'm new to Selenium, so can someone tell me, which of the two is faster and better? Is there another better locator technique that I can use? I've heard XPath is the default standard. Any reason why?
May 29, 2018 in Selenium by eLiJha
• 720 points
1,862 views

1 answer to this question.

0 votes
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. But it can be very slow, particularly in IE. Its also important to note that IE 6, 7, or 8, are slower with XPath when compared to Firefox. So, browser compatibility also makes a slight difference here and there.

So my take on this issue is, if you ever need to find an element using a complex selector, I would recommend using CSSSelectors, whenever possible. It's not quite as flexible as XPath, but will cover many of the same cases, without exhibiting extreme performance penalty on IE that XPath can.
answered May 29, 2018 by sniffy_god
• 780 points

Related Questions In Selenium

0 votes
1 answer

Which is better CSS or Xpath? Considering performance and cross browser testing

According to css has better performance compared ...READ MORE

answered Apr 17, 2018 in Selenium by Shubham
• 13,310 points
371 views
0 votes
1 answer

XPath for the elements with no ID or Name in Selenium Webdriver

Use like this or similar to this: //div[@id="top-level-menu-item-3"]/div[@class="filter-label"] //div[@id="top-level-menu1"] ...READ MORE

answered Apr 17, 2018 in Selenium by Meci Matt
• 9,420 points
1,473 views
0 votes
1 answer

What is the difference between dot and text() in XPath?

Even though there is some difference between ...READ MORE

answered Apr 25, 2018 in Selenium by king_kenny
• 3,650 points
1,571 views
0 votes
1 answer

What is the difference between relative and absolute XPath?

Absolute Xpath: It contains the complete path ...READ MORE

answered Dec 19, 2018 in Selenium by Nabarupa
2,620 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,110 points
995 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
1,148 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
890 views
0 votes
1 answer

I need to release the memeory allocated to Selenium chromedriver.exe while for tests

The usage of the commands is incorrect. driver.close() ...READ MORE

answered May 21, 2018 in Selenium by sniffy_god
• 780 points
931 views
0 votes
1 answer