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.

3
  • It is not possible for two elements to share the same Xpath.
    – Yu Zhang
    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
    – Sagar007
    Apr 24, 2017 at 10:27
  • Can you add the DOM screen of your application for this scenario? 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...

2
  • The result gave a IndexOutOfBoundException actually doesn't solve my problem 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!
    – corsiKa
    Dec 26, 2020 at 6:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.