python selenium click on button

▼魔方 西西 提交于 2019-11-26 02:35:49

问题


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.


回答1:


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()



回答2:


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:

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




回答3:


I had the same problem using Phantomjs as browser, so I solved in the following way:

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

Essentially I have added the name of the DIV tag into the quote.




回答4:


The following debugging process helped me solve a similar issue.

with open("output_init.txt", "w") as text_file:
    text_file.write(driver.page_source.encode('ascii','ignore'))


xpath1 = "the xpath of the link you want to click on"
destination_page_link = driver.find_element_by_xpath(xpath1)
destination_page_link.click()


with open("output_dest.txt", "w") as text_file:
    text_file.write(driver.page_source.encode('ascii','ignore'))

You should then have two textfiles with the initial page you were on ('output_init.txt') and the page you were forwarded to after clicking the button ('output_dest.txt'). If they're the same, then yup, your code did not work. If they aren't, then your code worked, but you have another issue. The issue for me seemed to be that the necessary javascript that transformed the content to produce my hook was not yet executed.

Your options as I see it:

  1. Have the driver execute the javascript and then call your find element code. Look for more detailed answers on this on stackoverflow, as I didn't follow this approach.
  2. Just find a comparable hook on the 'output_dest.txt' that will produce the same result, which is what I did.
  3. Try waiting a bit before clicking anything:

xpath2 = "your xpath that you are going to click on"

WebDriverWait(driver, timeout=5).until(lambda x: x.find_element_by_xpath(xpath2))

The xpath approach isn't necessarily better, I just prefer it, you can also use your selector approach.



来源:https://stackoverflow.com/questions/21350605/python-selenium-click-on-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!