[selenium-rc] How to press/click the button using Selenium if the button does not have the Id?

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>

The answer is


You can use xpath for for identifying that element.


You don't need to use only identifier as elements locators. You can use a few ways to find an element. Read this article and choose the best for you.


use the text and value attributes instead of the id

driver.findElementByXpath("//input[@value='cancel'][@title='cancel']").click();

similarly for Next.


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)

For Next button you can use xpath or cssSelector as below:

xpath for Next button: //input[@value='Next']

cssPath for Next button: input[value=Next]


    You can achieve this by using cssSelector 
    // Use of List web elements:
    String cssSelectorOfLoginButton="input[type='button'][id='login']"; 
    //****Add cssSelector of your 1st webelement
    //List<WebElement> button 
    =driver.findElements(By.cssSelector(cssSelectorOfLoginButton));
    button.get(0).click();

    I hope this work for you

Examples related to selenium-rc

Xpath for href element Page scroll up or down in Selenium WebDriver (Selenium 2) using java how to delete default values in text field using selenium? How to press/click the button using Selenium if the button does not have the Id? Clicking at coordinates without identifying element

Examples related to selenium-webdriver

SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 81 Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver is assuming that Chrome has crashed WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium? How to make Firefox headless programmatically in Selenium with Python? Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click How do you fix the "element not interactable" exception? Scrolling to element using webdriver? Only local connections are allowed Chrome and Selenium webdriver Check if element is clickable in Selenium Java

Examples related to selenium-ide

How to use XPath preceding-sibling correctly Running Selenium Webdriver with a proxy in Python Click a button with XPath containing partial id and title in Selenium IDE click command in selenium webdriver does not work Equivalent of waitForVisible/waitForElementPresent in Selenium WebDriver tests using Java? How to press/click the button using Selenium if the button does not have the Id? Does Google Chrome work with Selenium IDE (as Firefox does)? Selenium IDE - Command to wait for 5 seconds Click in OK button inside an Alert (Selenium IDE)

Examples related to buttonclick

RegisterStartupScript from code behind not working when Update Panel is used How to press/click the button using Selenium if the button does not have the Id? How to launch another aspx web page upon button click? How to make a button redirect to another page using jQuery or just Javascript