diff --git a/scrapling/core/mixins.py b/scrapling/core/mixins.py index 5cbcba4..868d620 100644 --- a/scrapling/core/mixins.py +++ b/scrapling/core/mixins.py @@ -26,7 +26,12 @@ class SelectorsGeneration: if target.parent: if target.attrib.get("id"): # id is enough - part = f"#{target.attrib['id']}" if css else f"[@id='{target.attrib['id']}']" + if css: + part = f"#{target.attrib['id']}" + elif full_path: + part = f"*[@id='{target.attrib['id']}']" + else: + part = f"[@id='{target.attrib['id']}']" selectorPath.append(part) if not full_path: return " > ".join(reversed(selectorPath)) if css else "//*" + "/".join(reversed(selectorPath)) diff --git a/tests/parser/test_general.py b/tests/parser/test_general.py index 3961660..293e93e 100644 --- a/tests/parser/test_general.py +++ b/tests/parser/test_general.py @@ -342,6 +342,11 @@ def test_full_path_selector_no_duplicate_ids(): assert len(result) == 1 assert result.first.text == "Hello" + # The generated XPath selector should also select the correct element + result = page.xpath(xpath_full) + assert len(result) == 1, f"XPath '{xpath_full}' selected {len(result)} elements, expected 1" + assert result.first.text == "Hello" + def test_full_path_selector_mixed_id_and_no_id(): """Test full path selectors with a mix of elements with and without ids"""