fix: emit valid XPath node test for ID elements in full-path mode

Address review feedback: In full-path XPath generation, elements with
IDs were producing bare predicates like `[@id='x']` which creates
invalid XPath steps like `//body/[@id='main']`. Now emits `*[@id='x']`
for full-path mode (e.g. `//body/*[@id='main']/*[@id='target']`).

Short-path XPath mode unchanged — still uses `//*[@id='x']` prefix.

Also added XPath evaluation assertion to the regression test to verify
the generated selector actually selects the correct element.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
sjhddh
2026-04-12 09:46:55 +02:00
parent 1ac26733d8
commit 273c8c2fa0
2 changed files with 11 additions and 1 deletions
+6 -1
View File
@@ -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))