What is the difference between absolute and relative xpaths? Which is preferred in Selenium automation testing?

前端 未结 3 1570
野趣味
野趣味 2020-12-01 18:51

What is the difference between absolute and relative xpaths? Which is preferred in Selenium automation testing? I am preparing test scripts using Selenium and Robot framewor

相关标签:
3条回答
  • 2020-12-01 19:07

    Consider Below Html

    <html>
      <body>
        <input type ="text" id="username">
      </body>
    
    </html>
    

    so Absoulte path= html/body/input and Relative path = //*[@id="username"]

    Disadvantage with Absolute xpath is maintenance is high if there is nay change made in html it may disturb the entire path and also sometime we need to write long absolute xpaths so relative xpaths are preferred

    0 讨论(0)
  • 2020-12-01 19:08

    An absolute xpath in HTML DOM starts with /html e.g.

    /html/body/div[5]/div[2]/div/div[2]/div[2]/h2[1]
    

    and a relative xpath finds the closed id to the dom element and generates xpath starting from that element e.g.

    .//*[@id='answers']/h2[1]/a[1]
    

    You can use firepath (firebug) for generating both types of xpaths

    It won't make any difference which xpath you use in selenium, the former may be faster than the later one (but it won't be observable)

    Absolute xpaths are prone to more regression as slight change in DOM makes them invalid or refer to a wrong element

    0 讨论(0)
  • 2020-12-01 19:19

    Absolute Xpath: It uses Complete path from the Root Element to the desire element.

    Relative Xpath: You can simply start by referencing the element you want and go from there.

    Relative Xpaths are always preferred as they are not the complete paths from the root element. (//html//body). Because in future, if any webelement is added/removed, then the absolute Xpath changes. So Always use Relative Xpaths in your Automation.

    Below are Some Links which you can Refer for more Information on them.

    • difference-between-absolute-path
    • xpath-tutorial-for-selenium
    • choosing-effective-xpath
    0 讨论(0)
提交回复
热议问题