Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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> 
like image 319
ChanGan Avatar asked Jan 15 '12 17:01

ChanGan


People also ask

How can you identify a button and click button in Selenium?

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.

How do I click a button using Selenium?

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.

What is the alternative way to click on Login button in Selenium?

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.


1 Answers

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) 
like image 136
Misha Akovantsev Avatar answered Sep 20 '22 10:09

Misha Akovantsev