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?

2
  • Does it have to be selenium? Commented Dec 14, 2013 at 12:43
  • I'm looking for a solution specifically using selenium. Thanks. Commented Dec 14, 2013 at 12:57

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The text attribute produces the visible text (i.e. what one sees when viewing a page) rather than the HTML code.
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.