3

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

halfer
  • 19,824
  • 17
  • 99
  • 186
Miquel Torres
  • 63
  • 3
  • 10
  • 1
    Possible duplicate of [How to press/click the button using Selenium if the button does not have the Id?](http://stackoverflow.com/questions/8871654/how-to-press-click-the-button-using-selenium-if-the-button-does-not-have-the-id) – Destrif Jun 29 '16 at 12:25
  • No, because the buttons are identical, – Miquel Torres Jun 29 '16 at 12:27

4 Answers4

4

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()
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • Fixed. :) You might be able to specify just the one element with the find but without more HTML I can't make the CSS selector that specific. This is at least a simple alternative. – JeffC Jun 29 '16 at 13:10
3

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

Destrif
  • 2,104
  • 1
  • 14
  • 22
  • I get a huge error, now i attached an image to the post :/ lets see if it helps – Miquel Torres Jun 29 '16 at 12:44
  • i only want to click on the second "Ir" button [link](https://s25.postimg.org/yuql7b9yn/Captura.png) <---- Capture – Miquel Torres Jun 29 '16 at 12:55
  • Nope :( i get this error "NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//form[@class='smallfont']/input[@value='Ir'][@type='submit'][2]"}" – Miquel Torres Jun 29 '16 at 13:06
  • As the other answer say(@Breaks Software), you do not need the second input. remove [2] at the end. – Destrif Jun 29 '16 at 13:07
1

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
Breaks Software
  • 1,721
  • 1
  • 10
  • 15
  • oh, but i still get a huge error :( 'ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with Stacktrace:' – Miquel Torres Jun 29 '16 at 13:03
  • 1
    Well, that's good, it means we found it! When you watch the test run, is that button actually visible? If not, then you'll need to add the necessary steps in the application so that it will be visible. If you do see it, then perhaps you need to use a WebDriverWait to wait for it to become visible before you click on it. – Breaks Software Jun 29 '16 at 13:08
-1

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.

sandeep kumar
  • 195
  • 1
  • 4
  • 20