8

I'm new to Selenium. Below is my code.

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

I have 3 buttons with the same type, id and value. How do I click on each of the buttons? Can anyone help me with the XPath?

Sanchit
  • 315
  • 2
  • 20
cxyz
  • 823
  • 8
  • 19
  • 37
  • 5
    It's *illegal* to have multiple elements with the same `id` in the same document. – reinierpost Nov 02 '12 at 06:52
  • that's true,... but i also encountered the same thing. 1. the component is inside the script 2. the component is on the layout (div). – gumuruh Feb 01 '17 at 08:47
  • there are many ways to do this, try this one https://pratikpathak.com/how-to-click-multiple-buttons-in-selenium/ – EvilReboot Nov 27 '22 at 11:40

7 Answers7

8

I resolved such problem in the following way:

String cssSelectorOfSameElements="input[type='submit'][id='button']";

 List<WebElement> a=driver.findElements(By.cssSelector(cssSelectorOfSameElements)) ;
 a.get(0).click();
//a.get(1).click();
//a.get(2).click();

depends upon what button you need to click on. Hope this works for you.

eugene.polschikov
  • 7,254
  • 2
  • 31
  • 44
5

use index based xpath like //input[1] and //input[2] and so on.

Abhishek_Mishra
  • 4,551
  • 4
  • 25
  • 38
  • selenium.click("xpath=//input[1][@id='button' and @value='Edit']"); i have tried this is not working – cxyz Oct 30 '12 at 07:00
  • Try it like selenium.click("//input[@id='button' and @value='Edit'][1]"); If still it does not work den try giving one attribute only without using 'and'.. – Abhishek_Mishra Oct 30 '12 at 09:06
2

There is one more simplest way through that we can find out the the uniquely xpath either we can generate the

indexing like xpath=(//input[@id='ndncchk'])[0] , xpath=(//input[@id='ndncchk'])[1], xpath=(//input[@id='ndncchk'])[2]

or we can find out the absolute xpath the way is :

got to firebug > open firebug >go to firepath >there will be a small dropdown list chose Genarate absolute xpath :

it will look like:

html/body/div[1]/form[1]/div[2]/div/div[2]/div[2]/div/div[3]/div[17]/div[2]/input[1]
html/body/div[1]/form[1]/div[2]/div/div[2]/div[2]/div/div[3]/div[17]/div[2]/input[2]...
Reporter
  • 3,897
  • 5
  • 33
  • 47
2

Identify the independent element First, post which you can identify the dependent element.

Say for example you have button next to Countries like India, USA, Australia. If you want to click on button next to USA then better write xpath for identifying USA and come one step backward in the html tree and identify the button which works 100% for everyone.

0

Try //input[@id='button' and @value='Edit'][1]. Generally I like to see what the parent nodes are and maybe specify the parents so they become unique.

KatieK
  • 13,586
  • 17
  • 76
  • 90
Nora
  • 1,432
  • 8
  • 9
0

This one worked for me when I tried locating the multiple combo boxes from chrome console.

$x("//select[@class='form-control']")[1]

It returned me the correct combo box with all the options underneath.

0

To resolve this, there are different ways

  1. use index[], //input[1] //input[2] //input[3]

  2. store the webelements into list and access with index

List buttonList=drive.findElements(By.id("button")); buttonList.get(0); and so on...

  1. Also,in some cases if we need to access specific element only then we can use 'Preceding-sibling' or 'following-sibling' tags
Dharman
  • 30,962
  • 25
  • 85
  • 135