Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python selenium click on button

I am quite new to python selenium and I am trying to click on a button which has the following html structure:

<div class="b_div">      <div class="button c_button s_button" onclick="submitForm('mTF')">         <input class="very_small" type="button"></input>         <div class="s_image"></div>         <span>            Search         </span>     </div>      <div class="button c_button s_button" onclick="submitForm('rMTF')" style="margin-bottom: 30px;">         <input class="v_small" type="button"></input>         <span>               Reset         </span>    </div>  </div> 

I would like to be able to click both the Search and Reset buttons above (obviously individually).

I have tried a couple of things, for example:

driver.find_element_by_css_selector('.button .c_button .s_button').click() 

or,

driver.find_element_by_name('s_image').click() 

or,

driver.find_element_by_class_name('s_image').click() 

but, I seem to always end up with NoSuchElementException, for example:

selenium.common.exceptions.NoSuchElementException: Message: u'Unable to locate element: {"method":"name","selector":"s_image"}' ; 

I am wondering if I can somehow use the onclick attributes of the HTML to make selenium click?

Any thoughts which can point me in the right direction would be great. Thanks.

like image 382
AJW Avatar asked Jan 25 '14 12:01

AJW


2 Answers

Remove space between classes in css selector:

driver.find_element_by_css_selector('.button .c_button .s_button').click() #                                           ^         ^ 

=>

driver.find_element_by_css_selector('.button.c_button.s_button').click() 
like image 173
falsetru Avatar answered Sep 30 '22 20:09

falsetru


try this:

download firefox, add the plugin "firebug" and "firepath"; after install them go to your webpage, start firebug and find the xpath of the element, it unique in the page so you can't make any mistake.

See picture: instruction

browser.find_element_by_xpath('just copy and paste the Xpath').click()

like image 35
Carlo 1585 Avatar answered Sep 30 '22 19:09

Carlo 1585