Examples
How to convert HTML to a base64 image with Selenium and Python
Install Selenium for Python
pip install selenium selenium 4.11+ includes Selenium Manager, which downloads the right chromedriver automatically — no separate driver install needed.
Get the base64 encoded image of some HTML and print to screen
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless=new')
driver = webdriver.Chrome(options=options)
html = '<html><body><h1>Hello World</h1></body></html>'
driver.get('data:text/html;charset=utf-8,' + html)
print(driver.get_screenshot_as_base64())
driver.quit() Find out how to generate a PNG from HTML with Python and Selenium.
That was 14 lines of code — plus a browser to run in production
Keeping headless browsers fed, patched and scaled is a job in itself. We built Urlbox so the same screenshot is one API call:
curl "https://api.urlbox.com/v1/YOUR_API_KEY/png?url=https://example.com" Try the Urlbox screenshot API — free trial, no browser babysitting.