Forums

Chrome Extensions in Playwright

I want to load my extension from directory.

Python version: 3.10.5

from playwright.sync_api import sync_playwright
from time import sleep

with sync_playwright() as playwright:
    user_data_dir_path = "./Profiles"
    extension_path = "./path_to_my_extension"
    browser_context = playwright.chromium.launch_persistent_context(
        user_data_dir_path,
        headless=False,
        executable_path="/usr/bin/chromium",
        args=[
            f"--disable-extensions-except={extension_path}",
            f"--load-extension={extension_path}",
            "--headless=new",
            "--disable-gpu",
            "--no-sandbox",
        ],
        ignore_default_args=["--disable-extensions"]
    )
    page = browser_context.pages[0]
    page.goto("chrome-extension://{extension_id}/{path}")
    # page.goto("chrome://extensions")
    sleep(5)
    page.screenshot(path="screenshot.png")

will break with:

=========================== logs ===========================
navigating to "chrome-extension://{extension_id}/{path}", waiting until "load"
============================================================

I'm using Playwright instead of Selenium so I couldn't try the link below. Chrome Extensions in Selenium

Any help would be greatly appreciated. Thank you!

First of all, PythonAnywhere is a headless environment, so I think headless=False (and related settings) will make it fail. Apart from that, do you see any errors in the output besides the "logs" part?

Thank you for your reply.

headless=False is so as to load the extension correctly. I'm using --headless=new instead. Below is the official Selenium documentation on this. Headless is Going Away!

Actually this simple test works:

from playwright.sync_api import sync_playwright

with sync_playwright() as playwright:
    browser = playwright.chromium.launch(executable_path="/usr/bin/chromium", args=["--headless=new"], ignore_default_args=[], headless=False)
    page = browser.new_page()
    page.goto("https://www.python.org")
    title = page.title()
    print(f"Title: {title}")
    browser.close()

Here is the full log:

(env) 01:33 ~/ProjectName $ python test.py
Traceback (most recent call last):
File "/home/USER_NAME/ProjectName/test.py", line 25, in <module>
    page.goto("chrome-extension://{extension_id}/{path}")
File "/home/USER_NAME/ProjectName/env/lib/python3.10/site-packages/playwright/sync_api/_generated.py", line 9297, in goto
    self._sync(
File "/home/USER_NAME/ProjectName/env/lib/python3.10/site-packages/playwright/_impl/_sync_base.py", line 109, in _sync
    return task.result()
File "/home/USER_NAME/ProjectName/env/lib/python3.10/site-packages/playwright/_impl/_page.py", line 479, in goto
    return await self._main_frame.goto(**locals_to_params(locals()))
File "/home/USER_NAME/ProjectName/env/lib/python3.10/site-packages/playwright/_impl/_frame.py", line 147, in goto
    await self._channel.send("goto", locals_to_params(locals()))
File "/home/USER_NAME/ProjectName/env/lib/python3.10/site-packages/playwright/_impl/_connection.py", line 61, in send
    return await self._connection.wrap_api_call(
File "/home/USER_NAME/ProjectName/env/lib/python3.10/site-packages/playwright/_impl/_connection.py", line 482, in wrap_api_call
    return await cb()
File "/home/USER_NAME/ProjectName/env/lib/python3.10/site-packages/playwright/_impl/_connection.py", line 97, in inner_send
    result = next(iter(done)).result()
playwright._impl._api_types.Error: net::ERR_ABORTED at chrome-extension://{extension_id}/{path}
=========================== logs ===========================
navigating to "chrome-extension://{extension_id}/{path}", waiting until "load"
============================================================

Is the page you're going to literally "chrome-extension://{extension_id}/{path}"? I would expect those things inside the {} to be the actual ID and path of an extension.

Yes. It sure works on my local machine.

Perhaps if you catch the exception, and then try a screenshot, you'll see some kind of error on the page saying why the extension is not loading?

The screenshot was blank. enter image description here

Code:

from playwright.sync_api import sync_playwright
from time import sleep

with sync_playwright() as playwright:
    user_data_dir_path = "./Profiles"
    extension_path = "./path_to_my_extension"
    browser_context = playwright.chromium.launch_persistent_context(
        user_data_dir_path,
        headless=False,
        executable_path="/usr/bin/chromium",
        args=[
            f"--disable-extensions-except={extension_path}",
            f"--load-extension={extension_path}",
            "--headless=new",
            "--disable-gpu",
            "--no-sandbox",
        ],
        ignore_default_args=["--disable-extensions"]
    )
    page = browser_context.pages[0]
    try:
        page.goto("chrome-extension://{extension_id}/{path}")
    except Exception as e:
        print(e)
        sleep(10)
        page.screenshot(path="error_message.png")
        exit()
    sleep(5)
    page.screenshot(path="screenshot.png")

By the way, the error did not occur when accessing other URLs (e.g. https://google.com). It seems that the error only occurs when accessing Chrome URLs (e.g. chrome://settings).

Or can I add extensions from the Chrome Web Store?

Would you be able to share a screenshot of what you see when you run it locally (with any private data obscured, of course)?

I can see My Extension page when accessing to chrome-extension://{extension_id}/{path}.

Here is a screenshot when accessing chrome://extensions. (Sorry for showing in Japanese.)

enter image description here

It look like extensions do not work correctly in headless mode at the moment.

After checking, it seems that the installed version is old.

(env) 23:22 ~/ProjectName $ /usr/bin/chromium --version
Chromium 90.0.4430.212 built on Debian 10.9, running on Debian bullseye/sid

So I tried to install the latest version (112 or newer). About --headless=new

However, as this site says, it was not possible because sudo apt install is not available.

Is there any way I use the new Chrome?

Unfortunately not; the only version that we support is the pre-installed one :-(

That’s unfortunate...

If possible, could you please update Chromium?

Thank you for providing many answer to my question!

That will be updated in our next system image, but we do not know when that will be releassed.

Thank you very much! I'm looking forward to it.