1

I am working on a web page which having 3 web elements with 3 enable buttons and all buttons having same Xpath.so my need is to click on the button A and print the title of the webelement on every login and logout operation.

Bharat Mane
6,78512 gold badges42 silver badges69 bronze badges
asked Apr 24, 2017 at 9:50
3
  • It is not possible for two elements to share the same Xpath. Commented Apr 24, 2017 at 10:12
  • Refer my answer in this link https://sqa.stackexchange.com/questions/26436/how-to-handle-if-xpath-is-same-for-multiple-drop-down/26439#26439 Commented Apr 24, 2017 at 10:27
  • Can you add the DOM screen of your application for this scenario? Commented Apr 24, 2017 at 10:33

1 Answer 1

5

In my case, How I worked on this same scenario: Suppose if you have 3 buttons with the same XPath-like in my DOM:

<input type="submit" id="button" value="Edit"/>
<input type="submit" id="button" value="Edit"/>
<input type="submit" id="button" value="Edit"/>

The best option to overcome this situation is:

1. By XPath indexing option:

By.xpath("(//input[@type='submit'])[0]") ---> To Click 1st Button
By.xpath("(//input[@type='submit'])[1]") ---> To Click 2nd Button
By.xpath("(//input[@type='submit'])[2]") ---> To Click 3rd Button

OR

XPath = "//input[@id='button' and @value='Edit'][0]
XPath = "//input[@id='button' and @value='Edit'][1]
XPath = "//input[@id='button' and @value='Edit'][2]

2. BY generating Absolute XPath from starting Node to Descent Node:

html/body/div[1]/form[1]/div[2]/div/div[2]/div[2]/input[0]
html/body/div[1]/form[1]/div[2]/div/div[2]/div[2]/input[1]
html/body/div[1]/form[1]/div[2]/div/div[2]/div[2]/input[2]

3. Use of List web elements:

 String cssSelectorOfBtn="input[type='submit'][id='button']"; 
 //****Add cssSelector of your 1st webelement
 List<WebElement> button =driver.findElements(By.cssSelector(cssSelectorOfBtn));
 button.get(0).click();
 button.get(1).click();
 button.get(2).click();

Let me know if it works...

answered Apr 24, 2017 at 10:57
2
  • The result gave a IndexOutOfBoundException actually doesn't solve my problem Commented Dec 25, 2020 at 22:11
  • @rabiacatak If you're having a problem, try to isolate it and, if necessary, ask a new question. Thanks! Commented Dec 26, 2020 at 6:09

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.