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

2

4 Answers 4

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()
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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

4 Comments

I get a huge error, now i attached an image to the post :/ lets see if it helps
i only want to click on the second "Ir" button link <---- Capture
Nope :( i get this error "NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//form[@class='smallfont']/input[@value='Ir'][@type='submit'][2]"}"
As the other answer say(@Breaks Software), you do not need the second input. remove [2] at the end.
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

2 Comments

oh, but i still get a huge error :( 'ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with Stacktrace:'
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.
-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.

2 Comments

No this is java. convert this code according to python.
I don not know python but this is the way you can resolve your issue, I am giving you an idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.