4

I have a situation where i had to handle drop down and click the drop down to select some values. There are two drop down in same page and both has same xpath. The Xpath is giving below.

//div[@name='expiryDate']/div/a/div[2]/i

I know only one way how to handle this, which is adding [1] with the xpath and I tried the below way to handle this.

//div[@name='expiryDate']/div/a/div[2]/i[1]

but it still it is not working.. Is there any other way i can try this?

Yu Zhang
9,9625 gold badges29 silver badges48 bronze badges
asked Mar 28, 2017 at 3:40
3
  • 2
    how is it not working? what exception have you got? Commented Mar 28, 2017 at 5:03
  • Please include the html in question with the two combo boxes Commented Mar 28, 2017 at 5:23
  • I have 3 webelement on web page but the xpath are same for all how can i get uniue xpath without using index?? please help me its very very urgent.. Commented Aug 22, 2023 at 11:09

2 Answers 2

5

It might be issue of same level HTML code.

Solution :

1. For First Dropdown :

(//div[@name='expiryDate']/div/a/div[2]/i)[1]

2. For Second Dropdown :

(//div[@name='expiryDate']/div/a/div[2]/i)[2]

3. Use element list to get Dropdown :

List<WebElement> lstDrp = driver.findElements(By.xpath("//div[@name='expiryDate']/div/a/div[2]/i"));
lstDrp.get(0).click();

4. For more specific and dynamic xpath use below code :

(//div[@name='expiryDate']//i)[1]
(//div[@name='expiryDate']//i)[2]

Please let me know If any issue.

answered Mar 28, 2017 at 5:23
0
-1

If an xpath refers multiple elements on the DOM, It should be surrounded by brackets first () and then use numbering.

if the xpath refers 4 elements in DOM and you need 3rd element then working xpath is (common xpath)[3].

If you are beginner and need more clarity watch https://youtu.be/k-znmPHjTF0

answered Apr 5, 2021 at 16:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.