stephanlensky / zendriver

A blazing fast, async-first, undetectable webscraping/web automation framework based on ultrafunkamsterdam/nodriver. Now with Docker support!
https://slensky.com/zendriver/
GNU Affero General Public License v3.0
41 stars 5 forks source link

How to load an unpacked chrome extension? #27

Open dinuka-dev opened 3 days ago

dinuka-dev commented 3 days ago

I need to load an unpacked chrome extension. How can i do that?

Below is my current code:

import asyncio, os
import zendriver as zd

extension_dir_name = "ext"
current_dir = os.path.dirname(os.path.abspath(__file__))
extension_dir = os.path.join(current_dir, extension_dir_name)

async def main():
    config = zd.Config()
    config.headless = False
    config.browser_args=[f'--load-extension={extension_dir}']
    browser = await zd.start(config)
    page = await browser.get("https://www.browserscan.net/")
    await page.save_screenshot("browserscan.png")
    input("Press Enter to close the browser...")
    await browser.stop()

if __name__ == "__main__":
    asyncio.run(main())
stephanlensky commented 2 days ago

Based on this Stack Overflow post, I think what you are doing should work.

It's possible that the answer is outdated though and the --load-extension flag no longer does anything.

The only other thing I would suggest is to try setting up a persistent user data directory by setting

config.user_data_dir = "/some/path/"

Then run your script to open the browser, manually add the extension, and close.

Hopefully then the extension will stay installed for subsequent runs.

therealpurplemana commented 2 days ago

--load-extension works. I am using it in my code with zendriver. Many extensions require a user confirmation now so that also has to be handled.

Here is the exact line that i use: browser_args.append(f"--load-extension={ghostery_extension_path}")