Miquel Torres
Miquel Torres

Reputation: 63

Click on second button (identical buttons) Selenium python

I have 2 buttons with the same value and class, they are identical.

<input class="button" value="Ir" type="submit">

I want to click the second one, that it is in <div class="smallfont">

How can i do that with python Selenium? Thanks ;D

INPUT CODE <------- IMAGE

Upvotes: 3

Views: 6208

Answers (4)

JeffC
JeffC

Reputation: 25542

You can grab references to both elements and just click the second one.

buttons = driver.find_elements_by_css_selector("input.button");
buttons[2].click()

Upvotes: 4

Breaks Software
Breaks Software

Reputation: 1761

If I am correct that there is only one submit button in the div of class "smallfont" (the duplicate button is in a different div that does not have that class), then you can simply use a path similar to:

//div[@class='smallfont']/input

Upvotes: 1

Destrif
Destrif

Reputation: 2134

Here is the example from the documentation:

<html>
 <body>
  <form id="loginForm">
   <input name="username" type="text" />
   <input name="password" type="password" />
   <input name="continue" type="submit" value="Login" />
   <input name="continue" type="button" value="Clear" />
  </form>
</body>
<html>

login_form = driver.find_element_by_xpath("/html/body/form[1]")
login_form = driver.find_element_by_xpath("//form[1]")
login_form = driver.find_element_by_xpath("//form[@id='loginForm']")

input[1] is an array, starting at 1, so in your case, it should looks like this(Corrected, following comment):

button = driver.find_element_by_xpath("//form[@class='smallfont']/input[@value='Ir'][@type='submit'][2]")
button.click()

From:

http://selenium-python.readthedocs.io/locating-elements.html

Upvotes: 3

sandeep kumar
sandeep kumar

Reputation: 195

Try to resolve in below 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();

Now depends on your requirement you can click on particular tab. Hope this works.

Upvotes: -1

Related Questions