selenium xpath and id

0 votes
Whats the difference between id and xpath, like while using xpath we write it as like: //input[@id='abc']

In this we are using id in xpath and this can also be done by simply using id like: By.id("abc"), so in xpath also we are using id and normally also id can be used to locate any element so whats the use of xpath?
Aug 9, 2020 in Selenium by Edureka
• 120 points
1,260 views

2 answers to this question.

0 votes

Hii @there,

First of all, id stands for identifier and are unique in a DOM. So, if you select a Web Element by id, you would definitely be able to target the element successfully.

Now xpath is a bit complicated. The general way of finding the xpath ( by using browser inpect element), shows the hierarchy of the element, so in case the DOM element changes in the future, your xpath expression would fail.

NOTE : There are a number of ways to create xpath expressions, which are pretty stale and less dependenct on the hierarchy of the web elements.

      Xpath=//tagname[@attribute='value'] 

      Relative xpath: //*[@class='featured-box']//*[text()='Testing'] 

    Such xpaths are pretty stable.As a rule, I will use xpath if a unique ID is not available, and I attempt to write it such that it is generic enough to resist "breakage."

    Thanks,

    answered Aug 10, 2020 by Niroj
    • 82,880 points
    0 votes
    id mean direct interacting with element like student id

    xpath mean : parent to child travel like son of abc is xyz
    answered Sep 3, 2020 by Sri
    • 3,190 points

    Related Questions In Selenium

    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
    6,834 views
    0 votes
    1 answer

    How to get next sibling element using XPath and Selenium for Java?

    Below code will help you: Try following-sibling axis : WebElement ...READ MORE

    answered May 15, 2018 in Selenium by Samarpit
    • 5,910 points
    20,253 views
    0 votes
    1 answer

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

    Technically speaking, By.ID() is the faster technique ...READ MORE

    answered May 30, 2018 in Selenium by sniffy_god
    • 780 points
    12,077 views
    0 votes
    2 answers

    what is the need of xpath when you have attributes like id ,class,name in selenium?

    some of the controls not have id ...READ MORE

    answered Sep 4, 2020 in Selenium by Sri
    • 3,190 points
    1,554 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
    12,579 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
    8,559 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
    6,606 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,507 views
    0 votes
    1 answer

    How to Handle alerts like Irctc alert in selenium?

    Hello  karthik, Handling alerts manually is a tedious task. ...READ MORE

    answered Jun 29, 2020 in Selenium by Niroj
    • 82,880 points
    2,422 views
    0 votes
    3 answers

    How to stop a page loading from selenium in chrome?

    Hi Savan, Setting the pageLoadStrategy capability to none. Then wait for ...READ MORE

    answered Jul 7, 2020 in Selenium by Suhana

    edited Jul 7, 2020 6,398 views
    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