Home > Back-end >  Download html code from element with xpath - Python Selenium
Download html code from element with xpath - Python Selenium

Time:09-24

I need download html codes from a element using xpath.

Xpath:

//*[@id=":nn"]/div[1]

And a picture from element:

enter image description here

How I can download these html codes ?

CodePudding user response:

You can use the the 'innerHTML' attribute after you get the element using the XPath. It would look something like:

element = driver.find_element_by_xpath("//*[@id=":nn"]/div[1]")`
content = element.get_attribute('innerHTML')

CodePudding user response:

I think by downloading you mean to get the innerHTML, if so

html = driver.find_element_by_xpath("//*[@id=':nn']/div[1]").get_attribute('innerHTML')
print(html)

update :

import pathlib
html = driver.find_element_by_xpath("//*[@id=':nn']/div[1]").get_attribute('innerHTML')
pathlib.Path("output.txt").write_text(f"Purchase Amount: {html}")

or

html = driver.find_element_by_xpath("//*[@id=':nn']/div[1]").get_attribute('innerHTML')

file = open("sample.html","w")
file.write(html)
file.close()
  • Related