12

I have a form with <input type="button" name="submit" /> button and would like to be able to click it.

I have tried mech.form.click("submit") but that gives the following error:

ControlNotFoundError: no control matching kind 'clickable', id 'submit'

mech.submit() also doesn't work since its type is button and not submit.

Any ideas? Thanks.

2 Answers 2

21

clicking a type="button" in a pure html form does nothing. For it to do anything, there must be javascript involved.

And mechanize doesn't run javascript.

So your options are:

  • Read the javascript yourself and simulate with mechanize what it would be doing
  • Use spidermonkey to run the javascript code

I'd do the first one, since using spidermonkey seems hard and probably not worth it.

Sign up to request clarification or add additional context in comments.

3 Comments

This was a long time ago, but what do you mean by "stimulate with mechanize"?
@nosklo yes.. sames question here.... how do you simulate javascript with mechanice?
@waas1919 you have to read the javascript code and understand what it does. For example, if the javascript code submits the form to a different address, then you write python code to do that manually. If the javascript code changes the value of some field, then you must do the change by hand in your python code.
1

Here is an example if the button is in a form:

import re
from mechanize import Browser
import requests
from bs4 import BeautifulSoup

browser = Browser()
browser.set_handle_robots(False)
browser.open("https://www.ecfrating.org.uk/v2/new/list_players.php")
browser.select_form(nr=0)
text = """Martins"""
browser['search'] = text
response = browser.submit()
response2=response.geturl()
print (response2) #to make sure that you moved to the desired url
browser.open(response2)
browser.select_form(nr=1)
print (browser) #to make sure that you have the right form
text = """A"""
browser['mode'] = [text,]
response = browser.submit()

soup = BeautifulSoup(response, "html.parser")
table = soup.find('table', {'class': ''})
data = soup.select("table")[0]
tab_data = [[item.text for item in row_data.select("th,td")]
            for row_data in data.select("tr")]
print (tab_data)

1 Comment

Thanks for this example. works perfectly and your real example provided context for the process and validation of the results in each of the steps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.