def __init__(self, source): if not source: source = "<html/>" parser = etree.HTMLParser(encoding="utf-8") tree = etree.HTML(source, parser=parser) for element in tree.xpath("//textarea"): content = inner_content(element) content = re.sub("\A\n", "", content) for child in element.getchildren(): element.remove(child) element.text = content self.tree = tree
def value(self): """ str: The value of the form element. """ if self.tag_name == "textarea": return inner_content(self.native) elif self.tag_name == "select": if self["multiple"] == "multiple": selected_options = self._find_xpath(".//option[@selected='selected']") return [_get_option_value(option) for option in selected_options] else: options = ( self._find_xpath(".//option[@selected='selected']") + self._find_xpath(".//option")) return _get_option_value(options[0]) if options else None elif self.tag_name == "input" and self["type"] in ["checkbox", "radio"]: return self["value"] or "on" else: return self["value"]
def _get_option_value(option): return option.get("value") or inner_content(option)