Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python mechanize - two buttons of type 'submit'

I have a mechanize script written in python that fills out a web form and is supposed to click on the 'create' button. But there's a problem, the form has two buttons. One for 'add attached file' and one for 'create'. Both are of type 'submit', and the attach button is the first one listed. So when I select the forum and do br.submit(), it clicks on the 'attach' button instead of 'create'. Extensive Googling has yielded nothing useful for selecting a specific button in a form. Does anyone know of any methods for skipping over the first 'submit' button and clicking the second?

like image 394
directedition Avatar asked Apr 09 '09 16:04

directedition


People also ask

Can I have two or more Submit buttons in the same form?

yes, multiple submit buttons can include in the html form. One simple example is given below.

How do I have two submit buttons?

Let's learn the steps of performing multiple actions with multiple buttons in a single HTML form: Create a form with method 'post' and set the value of the action attribute to a default URL where you want to send the form data. Create the input fields inside the as per your concern. Create a button with type submit.

How many submit buttons can be applied?

Two submit buttons in one form.

What is mechanize in Python?

The mechanize module in Python is similar to perl WWW:Mechanize. It gives you a browser like object to interact with web pages. Here is an example on how to use it in a program.


1 Answers

I tried using the nr parameter, without any luck.

I was able to get it to work with a combination of the name and label parameters, where "label" seems to correspond to the "value" in the HTML:

Here are my two submit buttons:

<input type="submit" name="Preview" value="Preview" /> <input type="submit" name="Create" value="Create New Page" /> 

... and here's the code that clicks the first one, goes back, and then clicks the second:

from mechanize import Browser self.br = Browser() self.br.open('http://foo.com/path/to/page.html') self.br.select_form(name='my_form') self.br['somefieldname'] = 'Foo' submit_response = self.br.submit(name='Preview', label='Preview') self.br.back() self.br.select_form(name='my_form') self.br['somefieldname'] = 'Bar' submit_response = self.br.submit(name='Create', label='Create New Page') 

There's a variant that also worked for me, where the "name" of the submit button is the same, such as:

<input type="submit" name="action" value="Preview" /> <input type="submit" name="action" value="Save" /> <input type="submit" name="action" value="Cancel" /> 

and

self.br.select_form(name='my_form') submit_response = self.br.submit(name='action', label='Preview') self.br.back() submit_response = self.br.submit(name='action', label='Save') 

IMPORTANT NOTE - I was only able to get any of this multiple-submit-button code to work after cleaning up some HTML in the rest of the page.

Specifically, I could not have <br/> - instead I had to have <br /> ... and, making even less sense, I could not have anything between the two submit buttons.

It frustrated me to no end that the mechanize/ClientForm bug I hunted for over two hours boiled down to this:

<tr><td colspan="2"><br/><input type="submit" name="Preview" value="Preview" />&nbsp;<input type="submit" name="Create" value="Create New Page" /></td></tr> 

(all on one line) did not work, but

<tr><td colspan="2"><br /> <input type="submit" name="Preview" value="Preview" /> <input type="submit" name="Create" value="Create New Page" /></td></tr> 

worked fine (on multiple lines, which also shouldn't have mattered).

I like mechanize because it was easy to install (just copy the files into my include directory) and because it's pretty simple to use, but unless I'm missing something major, I think that bugs like this are kind of awful - I can't think of a good reason at all why the first example there should fail and the second should work.

And, incidentally, I also found another mechanize bug where a <textarea> which is contained within a <p> is not recognized as a valid control, but once you take it out of the <p> container it's recognized just fine. And I checked, textarea is allowed to be included in other block-level elements like <p>.

like image 185
Chirael Avatar answered Sep 28 '22 05:09

Chirael