Last modified: Nov 28, 2023 By Alexander Williams

Python Selenium: Download HTML Page - Examples

Example 1: Download HTML Page Source


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Get the HTML source of the entire page
html_source = driver.page_source

# Print the HTML source
print(html_source)

# Close the browser
driver.quit()

Output:


# Full HTML source of the webpage

Example 2: Download HTML Page After Performing Actions


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Perform actions on the webpage (e.g., clicking a button)
# ...

# Get the HTML source after performing actions
html_source = driver.page_source

# Print the HTML source
print(html_source)

# Close the browser
driver.quit()

Output:


# HTML source after performing actions on the webpage

Example 3: Download HTML of a Specific Element


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Find a specific element on the webpage
element = driver.find_element_by_id('example-element')

# Get the HTML source of the specific element
html_source = element.get_attribute('outerHTML')

# Print the HTML source
print(html_source)

# Close the browser
driver.quit()

Output:


# HTML source of the specific element

Example 4: Download HTML Page Without Rendering JavaScript


from selenium import webdriver

# Create a Chrome driver with options to disable JavaScript
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-javascript')

driver = webdriver.Chrome(options=chrome_options)

# Navigate to a webpage
driver.get('https://example.com')

# Get the HTML source without rendering JavaScript
html_source = driver.page_source

# Print the HTML source
print(html_source)

# Close the browser
driver.quit()

Output:


# HTML source without rendering JavaScript

Example 5: Save HTML Page Source to a File


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Get the HTML source of the entire page
html_source = driver.page_source

# Save the HTML source to a file
with open('output.html', 'w', encoding='utf-8') as file:
    file.write(html_source)

# Close the browser
driver.quit()

Output:


# Saved output.html containing the HTML source

Example 6: Download HTML Page Using Requests Library


import requests
from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Get the current URL
url = driver.current_url

# Use the Requests library to download HTML
response = requests.get(url)
html_source = response.text

# Print the HTML source
print(html_source)

# Close the browser
driver.quit()

Output:


# HTML source downloaded using the Requests library