Package: chrome-extension-python
Chrome Extension PythonChrome Extension Pythonthat allows you to easily integrate Chrome extensions in web automation frameworks like Botasaurus, Selenium, and Playwright.This tool simplifies the process of downloading, configuring, and using any Chrome extension.InstallationInstall the package using pip:python-mpipinstallchrome_extension_pythonUsageThis package allows the use of Chrome extensions in Botasaurus, Selenium, and Playwright frameworks.Below are examples demonstrating the integration of the Adblock extension in each framework.Usage with BotasaurusBotasaurus, a web scraping framework, integrates easily withChrome Extension Python. Pass the Chrome Webstore link of the extension to use it.Example with Adblock Extension:frombotasaurusimport*fromchrome_extension_pythonimportExtension@browser(extensions=[Extension("https://chromewebstore.google.com/detail/adblock-%E2%80%94-best-ad-blocker/gighmmpiobklfepjocnamgkkbiglidom")],)defopen_chrome(driver:AntiDetectDriver,data):driver.prompt()open_chrome()Usage with SeleniumIntegration with Selenium involves setting up Chrome options and adding the extension.Example:fromseleniumimportwebdriverfromselenium.webdriver.chrome.optionsimportOptionsfromchromedriver_autoinstallerimportinstallfromchrome_extension_pythonimportExtension# Set Chrome optionsoptions=Options()options.add_argument(Extension("https://chromewebstore.google.com/detail/adblock-%E2%80%94-best-ad-blocker/gighmmpiobklfepjocnamgkkbiglidom").load())# Install and set up the driverdriver_path=install()driver=webdriver.Chrome(driver_path,options=options)# Prompt for user inputinput("Press Enter to exit...")# Clean updriver.quit()Usage with PlaywrightPlaywright integration includes specifying the extension path and launching the browser context with the extension.Example:fromplaywright.sync_apiimportsync_playwrightfromchrome_extension_pythonimportExtensionimportrandomdefgenerate_random_profile():returnstr(random.randint(1,1000))withsync_playwright()asp:extension_path=Extension("https://chromewebstore.google.com/detail/adblock-%E2%80%94-best-ad-blocker/gighmmpiobklfepjocnamgkkbiglidom").load(with_command_line_option=False)browser=p.chromium.launch_persistent_context(user_data_dir=generate_random_profile(),headless=False,args=['--disable-extensions-except='+extension_path,'--load-extension='+extension_path,],)page=browser.new_page()input("Press Enter to exit...")browser.close()Configuring an ExtensionExtensions can be configured eitherby editing their JavaScript filesor by interacting with their UI using Selenium.Editing JavaScript FilesThe JavaScript file editing method is faster and more robust. The following example demonstrates creating a configurable CapSolver Extension with an API key:fromchrome_extension_pythonimportExtensionclassCapsolver(Extension):def__init__(self,api_key):# Initialize the Capsolver extension with given parameterssuper().__init__(extension_id="pgojnojmmhpofjgdmaebadhbocahppod",# Unique identifier for the Chrome Extension, found in the Chrome Webstore linkextension_name="capsolver",# The name assigned to the extensionapi_key=api_key,# An important custom parameter (API key) required for the extension's functionality)# This method is called to update the necessary JavaScript files within the extensiondefupdate_files(self,api_key):# Retrieve a list of all JavaScript files in the extensionjs_files=self.get_js_files()defupdate_js_contents(content):# A string in the JavaScript file that needs to be replacedto_replace="return e.defaultConfig"# The new content to insert, which includes the API keyreplacement=(f"return{{...e.defaultConfig, apiKey: '{api_key}'}}")# Replace the old string with the new one in the file's contentreturncontent.replace(to_replace,replacement)# Loop through each JavaScript file and update its contentsforfileinjs_files:file.update_contents(update_js_contents)# Retrieve the specific configuration JavaScript fileconfig_file=self.get_file("/assets/config.js")defupdate_config_contents(content):# Replace the empty apiKey value with the new API key in the config filekey_replaced=content.replace("apiKey: '',",f"apiKey:'{api_key}',")returnkey_replaced# Update the config file with the new API keyconfig_file.update_contents(update_config_contents)Usage Example:frombotasaurusimport*@browser(extensions=[Capsolver(api_key="CAP-MY_KEY")],)defopen_chrome(driver:AntiDetectDriver,data):driver.get("https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php")driver.prompt()open_chrome()API ReferenceFor custom extension development, the following methods are available:get_js_files,get_json_files,get_html_files,get_css_files: Recursively retrieves specified file types.get_file: Retrieves a specific file as aFileobject.File.update_contents: Updates the content of a file.force_update(defaults to False): Redownload the extension and callupdate_fileswhen extension data changes. It is recommended to set it toTrueduring active development.Example of a custom extension withforce_update:fromchrome_extension_pythonimportExtensionclassCustomExtension(Extension):def__init__(self,api_key):# Initialize the CustomExtension with specific parameterssuper().__init__(extension_id="pgojnojmmhpofjgdmaebadhbocahppod",# Unique identifier for the Chrome Extension, obtained from the Chrome Webstore linkextension_name="capsolver",# The name assigned to the extensionforce_update=True,# This flag, when set to True, forces the redownload of the extension and calls the `update_files` method. This is useful during development to ensure updates are applied.)Examples of Custom ExtensionsHere are some code snippets of Custom Extensions to provide you with an idea of how to develop your own.Capsolver Extension2captcha ExtensionPublishing Your ExtensionIf you wish to share your extension with other developers via PyPI, follow these steps:Clone the template repository:capsolver-extension-python.Replace references to "capsolver" with your extension's name.Rename thecapsolver_extension_pythonfolder to match your extension name.Insert your extension code in__init__.py.Update README.mdUsenpm run uploadto publish the extension on PyPI.Contact us atchetan@omkar.cloudwith details about your extension. If it's beneficial for web scrapers, we'll promote it within the Botasaurus community.Love It?Star It ⭐!Become one of our amazing stargazers by giving us a star ⭐ on GitHub!It's just one click, but it means the world to me.