Multiple buttons with same type and id
Asked Answered
M

7

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?

Mongolic answered 30/10, 2012 at 6:24 Comment(3)
It's illegal to have multiple elements with the same id in the same document.Aeschylus
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).Shirtmaker
there are many ways to do this, try this one pratikpathak.com/how-to-click-multiple-buttons-in-seleniumStatic
N
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.

Narbada answered 30/10, 2012 at 9:29 Comment(0)
B
5

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

Buseck answered 30/10, 2012 at 6:38 Comment(2)
selenium.click("xpath=//input[1][@id='button' and @value='Edit']"); i have tried this is not workingMongolic
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'..Buseck
E
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]...
Eunuchoidism answered 16/7, 2014 at 12:13 Comment(0)
C
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.

Chard answered 1/8, 2018 at 6:16 Comment(0)
F
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.

Frowst answered 20/12, 2012 at 19:2 Comment(0)
O
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.

Oid answered 6/8, 2017 at 17:38 Comment(0)
P
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
Pituitary answered 19/8, 2020 at 13:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.