Autocomplete if name = hash

I am trying to autocomplete a text field (multiple boxes) in a form using mechanization in python, but the name of the field (s) is a hash, so I cannot automate input, for example, br.form [name '] =' blah ', since the name is an unknown hash from a hash function. Is there any way to do this? I looked online and could not find anything. Thanks!

+4
source share
3 answers

This should work for you. Obviously, you will need to update the predicate method. Also, do you have ongoing sex information? id, class, label, etc.

 import mechanize import re class MyBrowser: def __init__(self): self.user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)' self.cj = mechanize.LWPCookieJar() self.setup_browser(enable_debug=False) def find_by_crazy_name(self, control): if re.match('^[\w]{32,}$', control.name): return True return False def runit(self): self.agent.open('http://localhost') self.agent.select_form(name="foo") field = self.agent.form.find_control(predicate=self.find_by_crazy_name) field._value = "POOP" response = self.agent.submit() def enable_debug(self): self.agent.set_debug_http(True) self.agent.set_debug_redirects(True) self.agent.set_debug_responses(True) def setup_browser(self, enable_debug=False): self.agent = mechanize.Browser() self.agent.set_handle_redirect(True) self.agent.set_cookiejar(self.cj) self.agent.set_handle_referer(True) self.agent.set_handle_refresh(True) self.agent.set_handle_equiv(True) self.agent.set_handle_robots(False) self.enable_debug() self.agent.addheaders = [('User-Agent', self.user_agent)] if __name__ == "__main__": browser = MyBrowser() browser.runit() 

It just fills the entire potential POOP field. A field is a match if it is 32 alphanumeric characters (e.g. md5)

+1
source

Assuming br.form is a dictionary, you can key-click the default value for all fields in the form:

 for key in br.form: br.form[key] = 'blah' 

If you only want to fill in the default value for your unknown field, I assume that you recognize all the other field names so that you can do something like this:

 known_fields = set(['foo', 'bar']) # put your known keys in here for key in br.form: if key not in known_fields: # this must be the hash br.form[key] = 'blah' 

Note that this assumes your hash field already exists in br.form , possibly with None or an empty string value. I have not used mechanization, so I'm not sure if this is true.

0
source

If the requested form always has the same number of forms, you can find it by the form number (0 is the first form, etc.)

Try br.select_form(nr=number)

0
source

All Articles