Search code examples
pythonseleniumxpathhtml-parsinglxml

How to get an XPath from selenium webelement or from lxml?


I am using selenium and I need to find the XPaths of some selenium web elements.

For example:

import selenium.webdriver
driver = selenium.webdriver.Firefox()

element  = driver.find_element_by_xpath(<some_xpath>)
elements = element.find_elements_by_xpath(<some_relative_xpath>)

for e in elements:
    print e.get_xpath()

I know I can't get the XPath from the element itself, but is there a nice way to get it anyway?

I tried using lxml to parse the HTML, but it doesn't recognize the XPath, <some_xpath>, I passed, even though driver.find_element_by_xpath(<some_xpath>) did manage to find that element.


Solution

  • lxml can auto-generate an absolute xpath for you using getpath() method.

    Example (using wikipedia main page, getting xpath expression for the logo):

    import urllib2
    from lxml import etree
    
    data = urllib2.urlopen("https://en.wikipedia.org")
    tree = etree.parse(data)
    element = tree.xpath('//div[@id="p-logo"]/a')[0]
    print tree.getpath(element)
    

    Prints:

    /html/body/div[4]/div[2]/div[1]/a