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?
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?
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.
use index based xpath like //input[1] and //input[2] and so on.
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]...
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.
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.
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.
To resolve this, there are different ways
use index[], //input[1] //input[2] //input[3]
store the webelements into list and access with index
List buttonList=drive.findElements(By.id("button")); buttonList.get(0); and so on...
© 2022 - 2024 — McMap. All rights reserved.
id
in the same document. – Aeschylus