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
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.