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 8, 2020 in Selenium by Edureka
• 120 points
213 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 9, 2020 by Niroj
    • 82,720 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,010 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
    3,649 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,890 points
    15,096 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 29, 2018 in Selenium by sniffy_god
    • 780 points
    5,708 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,010 points
    779 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,520 points
    5,070 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,010 points
    5,812 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,890 points
    3,575 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 2, 2020 in Selenium by Sri
    • 3,010 points
    4,315 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,720 points
    492 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 6, 2020 in Selenium by Suhana

    edited Jul 6, 2020 1,841 views