0

I wanted to get full X-path of element instance using selenium python ?

Input:

element_inst = driver.find_element_by_link_text('Next')

I wanted to get full X-path of element_inst

Sample Output:

/html/body/div[1]/div[6]/div[1]/div[1]/div[1]/content-viewer/div/div/div/div[2]/div[2]/div/activity-viewer/div/div/phase-map-directive/div/div/div/ul/li[3]/div/span[1]

is there any way to print full X-path of element?

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Anonymous
  • 659
  • 6
  • 16
  • There is no option yet in selenium to fetch the `xpath` of the element.However if you want to fetch the html of the tag you can use `element_inst .get_attribute("outerHtml")` – KunduK Apr 21 '20 at 11:27
  • relaying on nested xpath is HORRIBLE practice and has to be forbidden. one change in the dom tree and your tests is failing. i can advice to read some basic xpath axes, the major usage of them is for complicated search or when relaying on text. for everyting else you can use css selectors, they are pretty straight forward. – Infern0 Apr 21 '20 at 12:29
  • You can use the javascript to achieve this as I discussed [here](https://stackoverflow.com/questions/56640816/generate-absolute-xpath-of-given-webelement-in-rselenium-r/56641268#56641268). Make sure you use `driver.exeucte_script` with javascript mentioned in the referred post. – supputuri Apr 21 '20 at 13:35

1 Answers1

0

Option -1 I found below link which may help you: https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/5520

Option 2- You can use Sikuli and record code for below steps and use clipboard data as xpath in your code

  • Right Click and click on inspect
  • On selected element just right click
  • Copy-> Xpath

Hope this helps

Amruta
  • 1,128
  • 1
  • 9
  • 19