Get Page Source in Selenium with page_source
In Selenium, the fastest way to inspect the markup of the page currently loaded in the browser is to read the WebDriver's page_source attribute.
Once the driver has opened a URL, that attribute exposes the rendered HTML as a string you can print, store, or hand to an HTML parser for extraction.
What page_source returns
Selenium exposes the current document through a single attribute on the driver object. You do not need a separate request or a view-source call: after navigation completes, reading that attribute gives you the HTML the browser is displaying, including markup added by JavaScript execution. That makes it the right choice when you want to inspect or parse exactly what the rendered page contains, rather than what a plain HTTP fetch would return.
Step-by-step Python workflow
The workflow has four ordered steps. First, import the Selenium webdriver module. Second, point a driver-path variable at the chromedriver executable on your machine and create a Chrome driver with that path. Third, navigate to the target address using the driver's get method, substituting your own URL for the placeholder. Fourth, read page_source from the same driver instance and print it or save it for parsing. Keeping the driver instance alive between navigation and the read is essential, because page_source reflects whichever page that browser session is currently showing. The complete sample is provided as protected module code0001.
Pre-run checklist
Use this checklist before running the workflow: confirm the browser driver executable exists at the path you configured; confirm the target URL is reachable from the machine running the script; read page_source only after navigation has been issued on the same driver; and decide in advance whether you will print the HTML, write it to a file, or feed it to a parser. If the output looks empty or unrelated, the most common cause is reading the attribute before navigation or from a different driver instance.
What to do next with the HTML
Getting the full HTML is usually only the first step. Once you have the markup, the natural follow-ups are locating specific elements with XPath or a CSS selector, waiting for dynamic content to finish loading, scrolling to off-screen elements, capturing a screenshot for visual confirmation, and managing cookies across sessions. Each of those tasks builds on the same driver session you already have open.
Frequently asked questions
Where does the HTML come from in Selenium?
Read the page_source property from the WebDriver instance after the browser has navigated to the target URL. That property returns the rendered HTML for the current page, which you can print, save, or pass to a parser.
What do I need before creating the driver?
Set the driver path to the location of your browser driver, such as chromedriver, and pass it when creating the Chrome driver. The example workflow uses a DRIVER_PATH placeholder for that location.
When should page_source be read?
Use the driver navigation method to open the address first, then access page_source. Reading the property before navigation would not give you the intended page.