Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium / lxml : Get xpath

Is there a get_xpath method or a way to accomplish something similar in selenium or lxml.html. I have a feeling that I have seen somewhere but can't find anything like that in the docs.

Pseudocode to illustrate:

browser.find_element_by_name('search[1]').get_xpath()

>>> '//*[@id="langsAndSearch"]/div[1]/form/input[1]'
like image 759
root Avatar asked Dec 16 '22 18:12

root


1 Answers

This trick works in lxml:

In [1]: el
Out[1]: <Element span at 0x109187f50>

In [2]: el.getroottree().getpath(el)
Out[2]: '/html/body/div/table[2]/tbody/tr[1]/td[3]/table[2]/tbody/tr/td[1]/p[4]/span'

See documentation of getpath.

like image 143
kavinyao Avatar answered Dec 28 '22 07:12

kavinyao