Find xpath from a webElement in python

Viewed 166

I'm trying to scrape some pages with python, working with Selenium, but I have a problem. The elements that I am looking for have different xpath, due to different page structures, but all are in the same part of the page with the same class name. So I'd like to know if there is the possibility, once I get the webElement, of knowing its class name, with the part of the page that I'm interested in, to find the XPath of this part of the page.

I'm doing this to find the part of the page:

interestedPart = driver.find_element_by_class_name("_45kb")

But after that I don't know how to find the "interestedPart" full XPath, can you help me?

Example of full XPath:

/html/body/div[1]/div/div[4...
1 Answers

yes, it's possible

element.get_attribute('class')

xpath = "//div[@class='"+element.get_attribute('class')+"']"

for your example Webelement called interestedPart:

xpath = "//div[@class='"+interestedPart.get_attribute('class')+"']"

Related