Lamps1829
Lamps1829

Reputation: 2451

Getting the HTML code of a particular div using selenium in python

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?

Upvotes: 1

Views: 2834

Answers (1)

James Mills
James Mills

Reputation: 19030

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.

Upvotes: 3

Related Questions