From 1507b26936b46d8613b3df197184a1fa1530afac Mon Sep 17 00:00:00 2001
From: doe1080 <98906116+doe1080@users.noreply.github.com>
Date: Mon, 23 Nov 2026 06:12:00 +0900
Subject: [PATCH] [utils] `find_xpath_attr`: Support quotes in attribute values
(#17857)
Authored by: doe1080
---
test/test_utils.py | 3 +++
yt_dlp/utils/_utils.py | 12 +++++++++---
2 files changed, 12 insertions(+), 3 deletions(-)
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):