Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def set_select(self, data):
"""Set the *selected*-attribute of the first option element
specified by ``data`` (i.e. select an option from a dropdown).
:param data: Dict of ``{name: value, ...}``.
Find the select element whose *name*-attribute is ``name``.
Then select from among its children the option element whose
*value*-attribute is ``value``. If no matching *value*-attribute
is found, this will search for an option whose text matches
``value``. If the select element's *multiple*-attribute is set,
then ``value`` can be a list or tuple to select multiple options.
"""
for (name, value) in data.items():
select = self.form.find("select", {"name": name})
if not select:
raise InvalidFormMethod("No select named " + name)
# Deselect all options first
for option in select.find_all("option"):
if "selected" in option.attrs:
del option.attrs["selected"]
# Wrap individual values in a 1-element tuple.
# If value is a list/tuple, select must be a <select multiple="">.
if not isinstance(value, list) and not isinstance(value, tuple):
value = (value,)
elif "multiple" not in select.attrs:
raise LinkNotFoundError("Cannot select multiple options!")
for choice in value:
option = select.find("option", {"value": choice})
</select>
form.set("eula-checkbox", True)
Example: uploading a file through a ``<input name="tagname" type="file">`` field (provide the path to the local file,
and its content will be uploaded):
.. code-block:: python
form.set("tagname") = path_to_local_file
"""
for func in ("checkbox", "radio", "input", "textarea", "select"):
try:
getattr(self, "set_" + func)({name: value})
return
except InvalidFormMethod:
pass
if force:
self.new_control('text', name, value=value)
return
raise LinkNotFoundError("No valid element named " + name)
def set_textarea(self, data):
"""Set the *string*-attribute of the first textarea element
specified by ``data`` (i.e. set the text of a textarea).
:param data: Dict of ``{name: value, ...}``.
The textarea whose *name*-attribute is ``name`` will have
its *string*-attribute set to ``value``.
"""
for (name, value) in data.items():
t = self.form.find("textarea", {"name": name})
if not t:
raise InvalidFormMethod("No textarea named " + name)
t.string = value
:param data: Dict of ``{name: value, ...}``.
In the family of checkboxes whose *name*-attribute is ``name``,
check the box whose *value*-attribute is ``value``. All boxes in
the family can be checked (unchecked) if ``value`` is True (False).
To check multiple specific boxes, let ``value`` be a tuple or list.
:param uncheck_other_boxes: If True (default), before checking any
boxes specified by ``data``, uncheck the entire checkbox family.
Consider setting to False if some boxes are checked by default when
the HTML is served.
"""
for (name, value) in data.items():
# Case-insensitive search for type=checkbox
checkboxes = self.find_by_type("input", "checkbox", {'name': name})
if not checkboxes:
raise InvalidFormMethod("No input checkbox named " + name)
# uncheck if requested
if uncheck_other_boxes:
self.uncheck_all(name)
# Wrap individual values (e.g. int, str) in a 1-element tuple.
if not isinstance(value, list) and not isinstance(value, tuple):
value = (value,)
# Check or uncheck one or more boxes
for choice in value:
choice_str = str(choice) # Allow for example literal numbers
for checkbox in checkboxes:
if checkbox.attrs.get("value", "on") == choice_str:
checkbox["checked"] = ""
break
def set_radio(self, data):
"""Set the *checked*-attribute of input elements of type "radio"
specified by ``data`` (i.e. select radio buttons).
:param data: Dict of ``{name: value, ...}``.
In the family of radio buttons whose *name*-attribute is ``name``,
check the radio button whose *value*-attribute is ``value``.
Only one radio button in the family can be checked.
"""
for (name, value) in data.items():
# Case-insensitive search for type=radio
radios = self.find_by_type("input", "radio", {'name': name})
if not radios:
raise InvalidFormMethod("No input radio named " + name)
# only one radio button can be checked
self.uncheck_all(name)
# Check the appropriate radio button (value cannot be a list/tuple)
for radio in radios:
if radio.attrs.get("value", "on") == str(value):
radio["checked"] = ""
break
else:
raise LinkNotFoundError(
"No input radio named %s with choice %s" % (name, value)
)
Example: filling-in a login/password form
.. code-block:: python
form.set_input({"login": username, "password": password})
This will find the input element named "login" and give it the
value ``username``, and the input element named "password" and
give it the value ``password``.
"""
for (name, value) in data.items():
i = self.form.find("input", {"name": name})
if not i:
raise InvalidFormMethod("No input field named " + name)
i["value"] = value