0

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.

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

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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