Some basic imports: >>> from lxml.html import usedoctest >>> from lxml.html.formfill import fill_form_html The simplest kind of filling is just filling an input with a value: >>> print(fill_form_html(''' ... ''', dict(foo='bar'))) You can also fill multiple inputs, like: >>> print(fill_form_html(''' ... ... ... ... ''', dict(foo=['bar1', 'bar2']))) Checkboxes can work either as boolean true/false, or be selected based on their inclusion in a set of values:: >>> print(fill_form_html(''' ... ... Would you like to be spammed? ...
... Spam you'd like to receive:
... Viagra spam: ...
... Stock spam: ...
... Other spam: ...
... ... ''', dict(spam_me=True, type=['viagra', 'other']))) Would you like to be spammed?
Spam you'd like to receive:
Viagra spam:
Stock spam:
Other spam:
FIXME: I need to test more of this. But I'm lazy and want to use the coverage report for some of this. This module also allows you to add error messages to the form. The errors add an "error" class to the input fields, and any labels if the field has a label. It also inserts an error message into the form, using a function you can provide (or the default function). Example:: >>> from lxml.html.formfill import insert_errors_html >>> print(insert_errors_html(''' ... ...
...
... label ...
...
... ... ... ... ... ''', { ... 'v1': "err1", ... 'v2': "err2", ... 'v3': [None, "err3-2"], ... 'v4': "err4", ... None: 'general error', ... '#fieldset': 'area error', ... }))
general error
area error
err1
label
err2
err3-2
err4
REGRESSION: When filling textareas, the "name" attribute used to be removed. The "name" attribute should be kept:: >>> print(fill_form_html(''' ... ...
    Initial value
... ''', dict(foo="Bar")))
    Bar