Not able to find the unique xpath for an element in expedia.com using Selenium



  • I am trying to find the unique xpath for elements in the "travelers" pop option in the home page of expedia.com

    enter image description here

    When I give the following xpath "//div[contains(@class,'gcw-menu')]/div/div1/div2/div[4]/button/span", there are 6 webelements related to this and I am not able to find the unique one that clicks the "+" to increase the adults.

    enter image description here

    I even tried

    java.util.List listel = driver.findElements(By.xpath("//div[contains(@class,'gcw-menu')]/div/div1/div2/div[4]/button/span"));

    but this does not work, since the xpath is still not unique.



  • If you want to click the element, then you have use the button tag:

    the xpath for the highlighted '+' sign is

    (//div[@class="traveler-selector-room-data target-clone-field"]//button[@class="uitk-step-input-button uitk-step-input-plus"] )[1]
    

    you can just change the number to 2, or 3 to get the other two.

    Approach i took:

    1) Find the parent element which is unique:

    Here, the frame is the unique element:

    enter image description here

    The locator for this frame is :

    //div[@class="traveler-selector-room-data target-clone-field"]
    

    2) Now find the unique child element button:

    Here, the locator for button was :

    //button[@class="uitk-step-input-button uitk-step-input-plus"]
    

    3) Now combine the two

    The button element is not direct child of parent element , so i used //

    Hence the final locator is :

    //div[@class="traveler-selector-room-data target-clone-field"]//button[@class="uitk-step-input-button uitk-step-input-plus"]
    

    But this gives 3 elements, we just need the first one:

    So,

    We put everything in () and then say we need first element 1

    (//div[@class="traveler-selector-room-data target-clone-field"]//button[@class="uitk-step-input-button uitk-step-input-plus"] )[1]
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2