From 8b32c26d1e23ea3278fd87d8b89c2a1090a3974b Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Sun, 12 Jun 2022 12:31:11 +0800 Subject: [PATCH] htmlparser: handle xpath pointing to element instead of text() --- nvchecker_source/htmlparser.py | 9 ++++++++- tests/test_htmlpasrer.py | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/nvchecker_source/htmlparser.py b/nvchecker_source/htmlparser.py index 077f1e0..104d511 100644 --- a/nvchecker_source/htmlparser.py +++ b/nvchecker_source/htmlparser.py @@ -25,10 +25,17 @@ async def get_version_impl(info): doc = html.fromstring(res.body, base_url=conf['url'], parser=parser) try: - version = doc.xpath(conf.get('xpath')) + els = doc.xpath(conf.get('xpath')) except ValueError: if not conf.get('missing_ok', False): raise GetVersionError('version string not found.') except etree.XPathEvalError as e: raise GetVersionError('bad xpath', exc_info=e) + + version = [ + str(el) + if isinstance(el, str) + else str(el.text_content()) + for el in els + ] return version diff --git a/tests/test_htmlpasrer.py b/tests/test_htmlpasrer.py index f3be52b..436c27b 100644 --- a/tests/test_htmlpasrer.py +++ b/tests/test_htmlpasrer.py @@ -19,3 +19,11 @@ async def test_xpath_missing_ok(get_version): "xpath": '//pre[@class="test-is-ok"]/text()', "missing_ok": True, }) is None + +async def test_xpath_element(get_version): + assert await get_version("unifiedremote", { + "source": "htmlparser", + "url": "http://httpbin.org/", + "xpath": '//pre[@class="version"]', + }) != None +