0

I'd like to know how could i select an option menu that IS NOT inside a form using Mechanize? The page i'm getting this data, has the selection option inside a div and not on a form

<select name="name" onchange="someJavaScript;">
    <option value="-1">Value -1</option>
    <option value="1">Value 1</option>
    <option value="2">Value 2</option>
</select>

2 Answers2

0

Would you mind providing us a link to the website?

Update: Not sure if you have solved the issue. I just got the following work for me, and believe it should work for you too:

# -*- coding: utf-8 -*- 
import mechanize

#-----------------------------------------------------------------------|
# The following fixes error "URLError: <urlopen error [Errno 8] _ssl.c:503".
# Include if necessary
import ssl
from functools import wraps
def sslwrap(func):
    @wraps(func)
    def bar(*args, **kw):
        kw['ssl_version'] = ssl.PROTOCOL_TLSv1
        return func(*args, **kw)
    return bar

ssl.wrap_socket = sslwrap(ssl.wrap_socket)
# -----------------------------------------------------------------------|

URL = 'https://search.yahoo.com/'
BASE_URL = 'https://search.yahoo.com/'

# The form/controls you'd like to work on, which can be extracted from source by clicking "Inspect" on the page
FORM_HTML = """
<form method="get" name="s" id="sf" role="search" action="https://search.yahoo.com/search;_ylt=AwrBTveB2p9Wj.IAEDDqFAx." accept-charset="utf-8"><label for="yschsp" class="off-left">Search query</label><div id="sbq-wrap" class="sbq-w"><input type="text" class="sbq" id="yschsp" name="p" value="" autocomplete="off" autofocus="" tabindex="1" autocorrect="off" autocapitalize="off" style="-webkit-tap-highlight-color: transparent;"><div id="yui_3_10_0_1_1453316701540_15" class="sa-sbx-container"><div id="yui_3_10_0_1_1453316701540_18" class="sa lowlight"></div></div></div><input type="submit" class="sbb" value="Search" tabindex="2"><input type="hidden" name="fr" value="sfp"><input type="hidden" name="fr2" value=""><input type="hidden" name="iscqry" id="iscqry"></form>
"""
br = mechanize.Browser()
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
br.open(URL)

# work on the FORM_HTML
res  = mechanize._form.ParseString(FORM_HTML, BASE_URL)

# get the HTMLForm instance
br.form = res[1]
form = br.form

# -----------------------------------------------
# Refer to: http://stackoverflow.com/questions/13965285/use-mechanize-to-submit-form-without-control-name
# In case the control's name is NONE:
form.set_value('stackover', type= 'text', id='yschsp')

# -----------------------------------------------
# In case the control has name 'p', use the following to set the value:
#form['p'] = 'stackoverflow'

# submit the form
response = mechanize.urlopen(form.click())

# print the page content
print response.read()
Quinn
  • 4,394
  • 2
  • 21
  • 19
0
# I don't have the page source. Hope it helps.
html = """<select name="name" onchange="someJavaScript;">
    <option value="-1">Value -1</option>
    <option value="1">Value 1</option>
    <option value="2">Value 2</option>
</select>"""

# work on the string above
forms  = mechanize.ParseString(html, 'fake')

# there is only one form here, form is HTMLForm instance
form = forms[0]

control = form.controls[0] # the select

# All values for control 'name'
print [item.attrs['value'] for item in control.items]   # ['-1','1','2']

# The current value for this control
print form['name']   # ['-1']

# Select a choice (e.g. choice with value '2')
form['name'] = ['2']

# Verify the value
print form['name']   # ['2']
Quinn
  • 4,394
  • 2
  • 21
  • 19
  • Just to get it right... You're saying that i can select a form to use the select option even though the select option is not inside a tag form? – Anderson Moura Jan 17 '16 at 12:21
  • Yes, you're right. In case there is no form you can find with Mechanize, I would suggest you try parsing/posting the data using Beautifulsoup & requests. Because mechanize only looks at form fields on a static HTML page and therefore fill/submit. – Quinn Jan 17 '16 at 14:52