Search code examples
xpathcucumber

Not able to select an element using xpath


Tried my level best, but I am not able to write a XPath that returns a unique element in this situation.

xpath 1: return all 7 elements under Default value, as all the elements have input tag

//div/span[contains(text(),'Default Values')]/ancestor::div/span[contains(text(),'Select Date')]/following-sibling::div//input

Xpath 2 returns 3 elements, the drop-down in the image.

//div[@class='_selectContainer_psmgei']/div//input

Screenshot: Window screenshot

HTML Screenshot: HTML code


Solution

  • If you want to get one unique input element, you need something like this :

    For the first XPath :

    //span[contains(text(),'Select Date')]/following::div[@class="Select-input"][1]/input
    

    Replace [1] with [2] or [3] to access the input element you want. I've replaced "following-sibling" from your expression since the span element has no div sibling.

    For the second XPath :

    (//div[@class='_selectContainer_psmgei'])[1]//input
    

    Replace [1] with [2] or [3] to access the input element you want.