XPath Generator Prompt
Generate a robust XPath locator from supplied HTML or XML, target requirements, namespace details, runtime constraints, and evidence that the result is unique.
Prompt Template
You are a senior test-automation engineer generating an XPath expression only from the HTML or XML and target behavior I supply. Do not invent attributes, ancestors, namespaces, text, or document structure. Prefer stable semantics over positional shortcuts. Document type: [HTML, XHTML, XML] Relevant sanitized markup: [paste the smallest complete subtree] Target node or nodes: [describe exact element and purpose] Required match count: [exactly one, all matching nodes, known number] XPath version: [1.0, 2.0, 3.1, unknown] Runtime: [browser document.evaluate, Selenium, Playwright, Appium, lxml, Saxon, other] Language binding: [JavaScript, Python, Java, C#, other] Stable attributes available: [id, name, role, data attribute, class token, other] Dynamic attributes or generated values to avoid: [list] Visible text policy: [stable exact text, normalized partial text, localized, dynamic, avoid text] Namespace prefixes and URIs: [details or none] Shadow DOM, iframe, or embedded-document boundary: [details or none] Sibling or ancestor relationships that are guaranteed: [details] Case-sensitivity and whitespace behavior: [details] Current locator and failure evidence: [expression, error, match count, changed markup] Browser or parser compatibility constraints: [details] Sensitive data removed from markup: [confirm] Provide: 1. A target inventory listing the supplied node, stable anchors, unstable details, document type, namespaces, and runtime limits. 2. One recommended XPath that meets the required match count using the narrowest stable evidence. 3. A short explanation of each step, predicate, function, and axis. 4. One conservative fallback when the preferred stable attribute is unavailable. 5. XPath 1.0 and later-version alternatives only when the named runtime makes the distinction relevant. 6. Correct namespace handling, including the resolver or prefix requirement where applicable. 7. A uniqueness and scope check against the supplied markup, clearly labeled as static analysis rather than executed proof. 8. Runtime-specific validation code or console steps that report the match count without clicking, submitting, deleting, or mutating anything. 9. Warnings for brittle positional indexes, generated classes, translated text, broad contains checks, iframe boundaries, and shadow roots. 10. A final audit for invented DOM facts, overbroad matches, invalid syntax, quote escaping, unsupported functions, and hidden document boundaries. Do not claim the XPath was executed unless actual output is supplied. Do not use absolute paths or numeric indexes when a stable semantic locator exists. If the markup is incomplete, return the missing subtree or namespace information needed instead of fabricating a selector.
Example Output
Recommended XPath
//button[@type='submit' and normalize-space(.)='[STABLE_BUTTON_TEXT]']
Why
- Selects button elements rather than every node with matching text.
- Requires the supplied stable type attribute.
- Normalizes surrounding whitespace in the supplied stable label.
Static Match Assessment
The provided subtree contains one matching button. This is not runtime proof; validate against the rendered document and each supported locale.
Read-Only Validation
Use the named runtime's locator-count method or evaluate the expression and inspect the number of returned nodes before any interaction.
Risk
If the label is localized or changes dynamically, replace the text predicate with a verified stable data attribute. An iframe or shadow root must be entered before XPath evaluation.
Tips for Best Results
- ๐กPaste the smallest complete subtree that includes the target and its stable ancestors.
- ๐กName the XPath version and runtime because supported functions and namespace handling differ.
- ๐กValidate match count without triggering an action before using the locator in automation.
Frequently Asked Questions
What is the XPath Generator Prompt prompt?
Generate a robust XPath locator from supplied HTML or XML, target requirements, namespace details, runtime constraints, and evidence that the result is unique. It's a free ChatGPT prompt template from our Coding collection โ copy it, fill in the bracketed variables, and paste it into your AI tool.
Which AI tools work with this prompt?
It's written and tested for ChatGPT, Claude and Gemini. Any AI assistant that accepts free-form text prompts will handle it well.
How do I customize this ChatGPT prompt?
Replace the bracketed variables โ such as [HTML, XHTML, XML], [paste the smallest complete subtree], [describe exact element and purpose] โ with your own details before running it. Paste the smallest complete subtree that includes the target and its stable ancestors.
Is this prompt free to use?
Yes. Every prompt on PromptAtlas is free to copy, customize, and use โ no signup required.
Related Prompts
Code Review Assistant
Get a thorough, senior-level code review with actionable feedback on quality, security, performance, and best practices.
Debugging Detective
Systematically debug errors and unexpected behavior with root cause analysis and fix suggestions.
Code Refactoring Advisor
Transform messy, complex code into clean, maintainable, well-structured code with clear explanations.