1

Is there any way (libs, not manually) for generating relative XPath for a known element in HTML?

Let say the second P element inside class="content"

<html>
    <body>
        <div class"title">
            <h1>***</h1>
        </div>
        <p> *** </p>
        <h3>***</h3>
        <div class"content">
            <p>****</p>
            <p>****</p>
        </div>
    </body>
</html>

Use case: The idea is to guess where are the elements that I might be interested in. For example title, content or author. After I've found the element I want to generate xpath for it

1
  • I wrote couple of chrome extensions to generate the xpaths for the elements but this seems to be a special requirement where you want to get the xpath based on other element. I am sure it's going to be little tricky but it's possible. Commented Mar 28, 2020 at 22:07

1 Answer 1

1

Try something like this:

from lxml import etree

datum = """
<html>
    <body>
        <div class="title">
            <h1>***</h1>
        </div>
        <p> *** </p>
        <h3>***</h3>
        <div class="content">
            <p>something</p>
            <p>target</p>
        </div>
    </body>
</html>
"""

root = etree.fromstring(datum)
tree = etree.ElementTree(root)
find_text = etree.XPath("//p[text()='target']")
for target in find_text(root):
    print(tree.getpath(target))

Output:

/html/body/div[2]/p[2]
Sign up to request clarification or add additional context in comments.

1 Comment

Lxml was my first option, As you can see in the output the XPath is absolute, not relative it seems I have to build it my self something like this Auto-generate XPaths using Python

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.