30

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>
ChanGan
  • 4,254
  • 11
  • 74
  • 135

7 Answers7

23

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)
Tamerz
  • 897
  • 1
  • 10
  • 25
Misha Akovantsev
  • 1,817
  • 13
  • 14
20

In Selenium IDE you can do:

Command   |   clickAndWait
Target    |   //input[@value='Next' and @title='next']

It should work fine.

faramka
  • 2,164
  • 1
  • 18
  • 21
5

use the text and value attributes instead of the id

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

similarly for Next.

user1710861
  • 413
  • 6
  • 9
2

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]

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
1

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.

faramka
  • 2,164
  • 1
  • 18
  • 21
0
    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
mkumar0304
  • 637
  • 7
  • 8
0

You can use xpath for for identifying that element.

Rohit Ware
  • 1,982
  • 1
  • 13
  • 29