Launching Selenium WebDriver Chrome Extension

I know how I can download the chrome extension in selenium webdriver. But I do not see posts / blogs that describe how I can run the chrome extension from Selenium.

I need to explicitly make chrome extension run / force it to execute a function from selenium. For example, I want to clear my Chrome browser cache using this extension using Selenium Webdriver.

Can I do this first? Or Selenium WebDriver will only help me with downloading the extension to the browser instance and leave it there?

+5
source share
2 answers

When the Chrome extension is activated, it already "works" (at least in its background / event page). There is no API for programmatically clicking a button.

If you want to make a little use of the functionality of an existing extension, I suggest downloading the source code of the extension and inserting an additional additional listener into the source code of the extension.

  • Get the extension source (for example, using the Chrome extension source viewer and the CRX viewer).
  • Unzip the zip file.
  • Create a new HTML file example_name.html and let it contain:

     <script src="example_name.js"></script> 
  • Create a new script file example_name.js and allow it to call extension functionality, for example:

     chrome.runtime.getBackgroundPage(function(bg) { // Relevant function at the background page. In your specific example: bg.clearCache(); }); 
  • Add the previous HTML file in web_accessible_resources to the manifest file.
  • Add the extension again, for example. using the GUI in chrome://extensions or using

     chrome.exe --pack-extension=directorycontainingextension 

    After creating directorycontainingextension.crx load this crx file in Chrome to find out the extension ID. If you don’t know how to load the crx file into Chrome, just visit https://robwu.nl/crxviewer/ , select the crx file, open the F12 developer tools and copy the 32-character string into the field “Calculated extension identifier: [extension identifier here ] ".

    (Starting with ChromeDriver 2.11, you can simply pin the extension instead of packing it as a CRX file and hard-code the extension identifier in the manifest file by setting the "key" attribute (this "key" attribute is also printed on the F12 console using the CRX viewer).)

After changing the extension, you will get an extension with the same functionality as the original, plus an additional HTML page. This new HTML page will reference the extension functionality when it opens .

After that, “starting” the extension is as simple as opening the chrome-extension://[EXTENSION ID HERE]/example_name.html in a new tab.

If you do not like these new tabs, you can also use the chrome.webRequest or chrome.declarativeWebRequest API to intercept custom URLs and activate the desired function whenever the page requests this resource. Then you can simply put the URL in <img> to activate the extension functionality.

+7
source

The above solution is technically justified, seems to be complicated, so I thought of a different way of doing this. Because many times I need a lot of things that are better to do manually, authentications, certain cookies, etc.

I use folders as profiles, I run:

 chrome_options = Options() chrome_options.add_argument("user-data-dir=selenium") driver = webdriver.Chrome(chrome_options=chrome_options) driver.get("www.google.com") 

Then I manually install the extensions and execute the logins that I need now, every time I start Webdriver with this folder, everything is there

 chrome_options = Options() chrome_options.add_argument("user-data-dir=selenium") driver = webdriver.Chrome(chrome_options=chrome_options) driver.get("www.google.com") #Now you can see the Extensions and the logins done are present 

The advantage is that you can use several folders with different settings and extensions without having to install and remove extensions, change settings, change logins, etc.

0
source

All Articles