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 30, 2018 in Selenium by eLiJha
• 770 points
12,664 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 30, 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,490 points
2,325 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,460 points
7,471 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,710 points
8,412 views
0 votes
3 answers

What is the difference between relative and absolute XPath?

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

answered Dec 20, 2018 in Selenium by Nabarupa
67,441 views
0 votes
2 answers

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,500 points
13,776 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,190 views
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,910 points
7,267 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,904 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
6,768 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP