Skip to content

Loading a Local HTML File in Playwright with Python

Playwright navigates to URLs, so a file on disk needs to become a file:// URL before navigation.

OUTPUT FORMATSHTMLJSONMarkdown

In Python, pathlib does both conversions for you: resolve() makes the location absolute, and as_uri() renders it as a valid URI that page.goto() can open.

Working example: relative path to file URI

Install Playwright and Chromium before running any example (protected module code0001). Then create a small HTML page (code0002) and a Python script beside it (code0003). The script builds the file URI from the relative name test.html, launches Chromium in headless mode, opens a page, navigates to that URI, and prints the text of the h1 element. Launch the script from the shared folder with the command in code0004; the expected printed line is shown in code0005.

Pre-flight checklist

Checklist before you run it: 1) test.html and demo.py sit in the same directory. 2) You run python3 demo.py from that directory, because the relative path is resolved from the working directory. 3) The h1 you print matches the heading inside test.html. If any of these is off, the failure is usually a path problem rather than a Playwright problem.

Direct file URI: Linux and Windows forms

Sometimes you already have the full URI and can skip pathlib entirely. On Linux, the filesystem path in code0006 corresponds to the URI in code0007, which you can pass straight to page.goto() as in code0008. On Windows, the path in code0009 corresponds to the URI in code0010, with forward slashes and the drive letter kept after file:///. Hand-writing these is workable for a one-off, but it is easy to get separators or the drive letter wrong.

Which approach should you choose?

Prefer Path.as_uri() for anything you will run more than once or share across machines. It applies the platform-specific formatting automatically, so the same script produces the correct URI on Linux and on Windows without you maintaining two string formats. Reserve the hand-written URI for a quick, single-machine check where you have already verified the exact string.

Related Playwright tasks

Once the local page is open, the usual next steps are waiting for load, selecting elements, and capturing output. Related Playwright questions cover waiting for a page to load, blocking resources, downloading files, finding elements by CSS selectors, and taking screenshots.

Choosing the input form
SituationRecommended input to page.goto()Why
Relative filename in the working directoryURI generated by Path.resolve().as_uri()Absolute, platform-correct URI without manual formatting
Known Linux pathfile:///home/user/Desktop/playwright-demo/test.htmlDirect URI works when already verified
Known Windows pathfile:///C:/Users/name/test.htmlForward slashes and drive letter after file:///

Examples use test.html and the paths shown in protected modules code0006-code0010.

Frequently asked questions

Can I pass a local path directly to page.goto()?

No. A plain filesystem path is not a URL. Convert it first: resolve it to an absolute location, then turn that into a file:// URI, and pass the URI to page.goto().

When is a hand-written file:// URI acceptable?

Use the generated URI when you can. It formats the path correctly for the current platform, including the different separators and drive-letter handling seen between Linux and Windows examples.

Why does a relative test.html path fail from another folder?

The relative name is interpreted from the directory where the script is launched, not from wherever the script file happens to sit. Keep the HTML file and script together and launch from that folder, or supply a path that is unambiguous from your working directory.