From 95886bdd259c57934cf24b2df03a39c5fd87e5e8 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Wed, 28 Feb 2018 06:59:14 -0500 Subject: [PATCH] clean up Signed-off-by: Kenneth Reitz --- requests_html.py | 13 +++++++------ tests/test_requests_html.py | 5 ++++- 2 files changed, 11 insertions(+), 7 deletions(-) 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()