Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you select choices in a form using Python?

I'd like to know how to select options in a form that is formatted like

  <td align="left">
                  <select name="FORM1" id="FORM1" multiple="multiple" size="5">
                      <option value="Value1">Value1</option>
                      <option value="Value2">Value2</option>
                  </select>
  </td>

Right now, I am using mechanize to connect to the website and traverse to the desired page. This page has many forms such as FORM1, FORM2, FORM3, etc. with options. I'd like to select (enable) Value1 then tell the instance of mechanize to hit the submit button. Which would be a quick way to enable an option based on the form name?

like image 430
user791953 Avatar asked Jun 15 '11 15:06

user791953


People also ask

How do you select multiple items from a list in Python?

Select multiple random choices from a list The choice () function only returns a single item from a list. If you want to select more than one item from a list or set, use random sample () or choices () instead. random.choices(population, weights=None, *, cum_weights=None, k=1)

What is the use of selection statement in Python?

The selection statements are used to select a part of the program to be executed based on a condition. Python provides the following selection statements. if statement. if-else statement. if-elif statement.

How do you randomly select multiple choices in Python?

Randomly select multiple choices. As you know, the random.choice () function only returns a single item from a list. If you want to randomly select more than one item from a list or set, I’d recommend using a random.choices() or random.sample () instead.

What is the return type of choice () method in Python?

Returns: The choice () returns a random item. Note:We have to import random to use choice () method. Below is the Python3 implementation of the above approach: The output every-time will be different as the system returns a random item.


1 Answers

Here are some basic usage examples to get you going:

>>> import mechanize
>>> br = mechanize.Browser()
>>> br.open('http://www.w3schools.com/html/html_forms.asp')

Forms have a name attribute; sometimes it's empty though:

>>> [f.name for f in br.forms()]
['searchform', None, None, None, None, 'input0']

Forms have a sequence of controls; controls also have names:

>>> forms = [f for f in br.forms()]
>>> forms[1].controls[0].name
'firstname'
>>> [c.name for c in forms[3].controls]
['sex']

You can get a listing of items in a control:

>>> forms[3].controls[0].get_items()
[<Item name='male' id=None type='radio' name='sex' value='male'>, <Item name='female' id=None type='radio' name='sex' value='female'>]

For radio buttons, you have to make a single selection:

>>> forms[3]['sex'] = ['male']

But the selection has to be in a list:

>>> forms[3]['sex'] = 'male'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.6/site-packages/mechanize/_form.py", line 2782, in __setitem__
    control.value = value
  File "/Library/Python/2.6/site-packages/mechanize/_form.py", line 1977, in __setattr__
    self._set_value(value)
  File "/Library/Python/2.6/site-packages/mechanize/_form.py", line 1985, in _set_value
    raise TypeError("ListControl, must set a sequence")
TypeError: ListControl, must set a sequence

For check boxes you can make multiple selections:

>>> [(c.name, c.get_items()) for c in forms[4].controls]
[('vehicle', [<Item name='Bike' id=None type='checkbox' name='vehicle' value='Bike'>, <Item name='Car' id=None type='checkbox' name='vehicle' value='Car'>])]
>>> forms[4]['vehicle'] = ['Bike', 'Car']

You can find more info here (link stolen from Matt Hempel :).

like image 123
senderle Avatar answered Nov 15 '22 18:11

senderle