diff --git a/test/test_utils.py b/test/test_utils.py index 5dc8cbe017..a4dcffd52d 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -527,6 +527,7 @@ class TestUtil(unittest.TestCase): + ''' doc = compat_etree_fromstring(testxml) @@ -541,6 +542,8 @@ class TestUtil(unittest.TestCase): self.assertEqual(find_xpath_attr(doc, './/node', 'y', 'c'), doc[2]) self.assertEqual(find_xpath_attr(doc, './/node', 'y', 'd'), doc[3]) self.assertEqual(find_xpath_attr(doc, './/node', 'x', ''), doc[4]) + self.assertEqual(find_xpath_attr(doc, './/node', 'x', 'a\'b"c'), doc[5]) + self.assertEqual(find_xpath_attr(doc, './/node', 'y', 1), doc[5]) def test_xpath_with_ns(self): testxml = ''' diff --git a/yt_dlp/utils/_utils.py b/yt_dlp/utils/_utils.py index 8178d4e808..3e7006a8f9 100644 --- a/yt_dlp/utils/_utils.py +++ b/yt_dlp/utils/_utils.py @@ -234,9 +234,15 @@ def partial_application(func): def find_xpath_attr(node, xpath, key, val=None): """ Find the xpath xpath[@key=val] """ - assert re.match(r'^[a-zA-Z_-]+$', key) - expr = xpath + (f'[@{key}]' if val is None else f"[@{key}='{val}']") - return node.find(expr) + assert re.fullmatch(r'[a-zA-Z_-]+', key) + expr = f'{xpath}[@{key}]' + if val is None: + return node.find(expr) + + val = str(val) + for n in node.iterfind(expr): + if n.get(key) == val: + return n def xpath_with_ns(path, ns_map):