How to get page source in Selenium?

When web scraping, we often want to retrieve full page source (full HTML of the web page) we can parse it for data using tools like BeautifulSoup. Using Python and Selenium, to get the page source we can use driver.page_source attribute:

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://httpbin.org/html")
print(driver.page_source)

⚠ It's possible that this command will retrieve page source before the page fully loads if it's a dynamic javascript page. For more see How to wait for page to load in Selenium?

Related Posts

How to Scrape Google Maps

We'll take a look at to find businesses through Google Maps search system and how to scrape their details using either Selenium, Playwright or ScrapFly's javascript rendering feature - all of that in Python.

Web Scraping with Selenium and Python Tutorial + Example Project

Introduction to web scraping dynamic javascript powered websites and web apps using Selenium browser automation library and Python.

Scraping Dynamic Websites Using Web Browsers

Introduction to using web automation tools such as Puppeteer, Playwright, Selenium and ScrapFly to render dynamic websites for web scraping