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.dev/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?

Question tagged: Selenium, Headless Browsers, Python

Related Posts

How to Scrape With Headless Firefox

Discover how to use headless Firefox with Selenium, Playwright, and Puppeteer for web scraping, including practical examples for each library.

Selenium Wire Tutorial: Intercept Background Requests

In this guide, we'll explore web scraping with Selenium Wire. We'll define what it is, how to install it, and how to use it to inspect and manipulate background requests.

Web Scraping Dynamic Web Pages With Scrapy Selenium

Learn how to scrape dynamic web pages with Scrapy Selenium. You will also learn how to use Scrapy Selenium for common scraping use cases, such as waiting for elements, clicking buttons and scrolling.