1

Using Selenium in Python, I'd like to download a page, and save the HTML code of a particular div, identified by its id. I've got the following:

from selenium.webdriver import Firefox 
from selenium.webdriver.support.ui import WebDriverWait

...

with closing(Firefox()) as browser:
   browser.get(current_url)

WebDriverWait(browser, timeout=3).until(lambda x: x.find_element_by_id('element_id'))

element = browser.find_element_by_id('element_id')

element is of type selenium.webdriver.remote.webelement.WebElement. Is there a way to get the HTML code (not processed in any way) from element? Is there some better way, using Selenium, of accomplishing this task?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Lamps1829
  • 2,231
  • 3
  • 24
  • 32

1 Answers1

3

Right from pydoc selenium.webdriver.remote.webelement.WebElement:

 |  text
 |      Gets the text of the element.

Use the .text attribute.

If you really are after the HTML source of the element then please see: Get HTML Source of WebElement in Selenium WebDriver using Python

As stated above, it's not as straight forward as you'd like.

Community
  • 1
  • 1
James Mills
  • 18,669
  • 3
  • 49
  • 62
  • The text attribute produces the visible text (i.e. what one sees when viewing a page) rather than the HTML code. – Lamps1829 Dec 14 '13 at 13:07
  • 1
    A variant of this reply from the thread you linked above took care of it: elem = driver.find_element_by_xpath("//*"); source_code = elem.get_attribute("innerHTML"). Thanks! – Lamps1829 Dec 14 '13 at 13:34