Home > OS >  Is it possible to get the xpath (source) of an element using selenium in python?
Is it possible to get the xpath (source) of an element using selenium in python?

Time:09-14

If you take a look at this site, you will see the title/text "Example Domain". Is it possible to get its xpath which is /html/body/div/h1 using selenium? Is there any other possibilities? I mean I want to get xpath itself and not its content! I know we can get the page_soruce using driver.page_source but this not what I'm looking for. I simply expect an output as /html/body/div/h1.

I tried this:

test = driver.page_source
ps = str(test)
root = etree.fromstring(ps)
tree = etree.ElementTree(root)

find_text = etree.XPath("//p[text()='my_target_text']") # in our case Example Domain

for target in find_text(root):
    print(tree.getpath(target))

It returns:

lxml.etree.XMLSyntaxError: Opening and ending tag mismatch

CodePudding user response:

What you need (based on how you worded your question: I honestly doubt this is what you really need, and I'm sure that if you would state your end goal, someone would put you on the right path) is this:

https://gist.github.com/ergoithz/6cf043e3fdedd1b94fcf

I figured this would actually represent a full answer to your question as asked, so posting it as a response.

CodePudding user response:

I don't think it is possible directly but you can get the HTML source and then from that maybe go back to the xpath of the element you're looking for.

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("https://example.com/")
page_source = driver.page_source
print(page_source)

Output:

<html><head>
    <title>Example Domain</title>

    <meta charset="utf-8">
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style type="text/css">
    body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
        
    }
    div {
        width: 600px;
        margin: 5em auto;
        padding: 2em;
        background-color: #fdfdff;
        border-radius: 0.5em;
        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
    }
    a:link, a:visited {
        color: #38488f;
        text-decoration: none;
    }
    @media (max-width: 700px) {
        div {
            margin: 0 auto;
            width: auto;
        }
    }
    </style>    
</head>

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is for use in illustrative examples in documents. You may use this
    domain in literature without prior coordination or asking for permission.</p>
    <p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>


</body></html>
  • Related