Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 628
ro.e Avatar asked Jun 25 '14 14:06

ro.e


People also ask

Can we get XPath from Webelement?

Go to the First name tab and right click >> Inspect. On inspecting the web element, it will show an input tag and attributes like class and id. Use the id and these attributes to construct XPath which, in turn, will locate the first name field.

What is the best way to identify XPath?

To identify the element with xpath, the expression should be //tagname[@attribute='value']. To identify the element with xpath, the expression should be //tagname[@class='value']. There can be two types of xpath – relative and absolute.


1 Answers

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
like image 184
alecxe Avatar answered Sep 18 '22 16:09

alecxe