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.

nunos
  • 20,479
  • 50
  • 119
  • 154

2 Answers2

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.

nosklo
  • 217,122
  • 57
  • 293
  • 297
  • 2
    This was a long time ago, but what do you mean by "stimulate with mechanize"? – Constantly Confused Jan 13 '16 at 13:30
  • @nosklo yes.. sames question here.... how do you simulate javascript with mechanice? – waas1919 Apr 29 '16 at 16:19
  • 1
    @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. – nosklo May 06 '16 at 20:10
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)
Al Martins
  • 431
  • 5
  • 13
  • 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. – Programmer66 Dec 21 '22 at 19:03