mechanize python click a button

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x