Filling a text box with the Python mechanization module

Is there a way to populate a text box that is part of a form using the mechanization module for Python?

+4
source share
3 answers

forms link contains several examples of populating text controls in response objects.

Relevant quote:

 # The kind argument can also take values "multilist", "singlelist", "text", # "clickable" and "file": # find first control that will accept text, and scribble in it form.set_value("rhubarb rhubarb", kind="text", nr=0) 

The kind argument can be used with the form.find_control() and form.set_value() methods to search for "text" controls.

Digging a bit into mechanize the _form.py source . We have an explanation. Mechanize TextControl covers (among others) a TEXTAREA form TEXTAREA .

 #--------------------------------------------------- class TextControl(ScalarControl): """Textual input control. Covers: INPUT/TEXT INPUT/PASSWORD INPUT/HIDDEN TEXTAREA """ def __init__(self, type, name, attrs, index=None): ScalarControl.__init__(self, type, name, attrs, index) if self.type == "hidden": self.readonly = True if self._value is None: self._value = "" def is_of_kind(self, kind): return kind == "text" 
+5
source

You can do something like

 import mechanize br = mechanize.Browser() br.open("http://pypi.python.org/pypi") br.select_form("searchform") br['term'] = "Mechanize" response = br.submit() 

br['term'] = "Mechanize" is the corresponding string.

And you really need to accept some answers to your questions.

+6
source

you can first view the form of the element and how many forms on the page can be done with

 for form in br.forms(): print form 
+1
source

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


All Articles