I have 2 buttons Cancel and Next button on the same page but it has only one id (see the below code). I wanted to press Next but every time it is identifying the cancel button only not Next button. How to resolve this issue?
<td align="center"> <input type="button" id="cancelButton" value="Cancel" title="cancel" class="Submit_Button" style="background-color: rgb(0, 0, 160);"> <input type="submit" value="Next" title="next" class="Submit_Button"> </td>
We can find the button on the web page by using methods like find_element_by_class_name(), find_element_by_name(), find_element_by_id() etc, following which we can click on it by using the click() method.
We can click a button with Selenium webdriver in Python using the click method. First, we have to identify the button to be clicked with the help of any locators like id, name, class, xpath, tagname or css. Then we have to apply the click method on it. A button in html code is represented by button tagname.
Alternative of click() in Selenium We can use the JavaScript Executor to perform a click action. Selenium can execute JavaScript commands with the help of the executeScript method. The parameters – arguments[0]. click() and locator of the element on which the click is to be performed are passed to this method.
Use xpath selector (here's quick tutorial) instead of id:
#python: from selenium.webdriver import Firefox YOUR_PAGE_URL = 'http://mypage.com/' NEXT_BUTTON_XPATH = '//input[@type="submit" and @title="next"]' browser = Firefox() browser.get(YOUR_PAGE_URL) button = browser.find_element_by_xpath(NEXT_BUTTON_XPATH) button.click()
Or, if you use "vanilla" Selenium, just use same xpath selector instead of button id:
NEXT_BUTTON_XPATH = '//input[@type="submit" and @title="next"]' selenium.click(NEXT_BUTTON_XPATH)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With