Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6037921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:08:38+00:00 2026-05-23T06:08:38+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:08:39+00:00Added an answer on May 23, 2026 at 6:08 am

    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 :).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.