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.
1 Answer
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...
-
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
https://sqa.stackexchange.com/questions/26436/how-to-handle-if-xpath-is-same-for-multiple-drop-down/26439#26439