diff --git a/requests_html.py b/requests_html.py index 08d2370..d4ba93f 100644 --- a/requests_html.py +++ b/requests_html.py @@ -136,12 +136,13 @@ class BaseParser: :class:`Element ` found. """ selected = self.lxml.xpath(selector) - try: - c = [Element(element=e, url=self.url, default_encoding=_encoding or self.encoding) for e in selected] - # Sanity check. - [e.keys for e in c] - except AttributeError: - c = selected + c = [] + for selection in selected: + if not isinstance(selection, etree._ElementUnicodeResult): + element = Element(element=selection, url=self.url, default_encoding=_encoding or self.encoding) + else: + element = selection + c.append(element) if first: try: diff --git a/tests/test_requests_html.py b/tests/test_requests_html.py index c2c9ad7..8a3c871 100644 --- a/tests/test_requests_html.py +++ b/tests/test_requests_html.py @@ -59,6 +59,9 @@ def test_xpath(): html = r.html.xpath('/html', first=True) assert 'no-js' in html.attrs['class'] + a_hrefs = r.html.xpath('//a/@href') + print(a_hrefs) + def test_html_loading(): doc = """""" @@ -77,4 +80,4 @@ def test_anchor_links(): if __name__ == '__main__': - test_anchor_links() + test_xpath()