Python mechanism with dynamic drop-down list

I use mechanize to fill out forms, and I'm having a problem with dynamically populated drop-down lists, which depend on the previous selection.

In mechanization, I do something like this to select a category:

import mechanize
br = mechanize.Browser()
"""Select the form and set up the browser"""
br["state"] = ["California"]
br["city"] = ["San Francisco"] # this is where the error is
br.submit()

I cannot select a city as “San Francisco” until I select a state as “California” because the city drop-down list is dynamically populated after selecting “California”.

How can I send a city using Python and mechanize?

+3
source share
1 answer

mechanize JavaScript. urllib2 .

import urllib2
import urllib

values = dict(state="CA", city="SF") # examine form for actual vars
try:
    req = urllib2.Request("http://example.com/post.php",
                          urllib.urlencode(values))
    response_page = urllib2.urlopen(req).read()
except urllib2.HTTPError, details:
    pass #do something with the error here...
+1

Source: https://habr.com/ru/post/1772600/


All Articles