Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

.net - Running a chrome extension from Selenium WebDriver

I am aware of how I can load a chrome extension in selenium webdriver. But I am not seeing any posts/blogs which describe how I can run a chrome extension from Selenium.

I need to explicitly make a chrome extension run/make it perform it's function from selenium. For example, I want to clear the cache of Chrome browser using this extension with Selenium Webdriver.

Can I do it in the first place? Or will Selenium WebDriver help me only with loading an extension into the browser instance and leave it there?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When a Chrome extension is activated, it is already "running" (its background/event page, at least). There is no API to programatically click on the button.

If you want to use the functionality of an existing extension with little efforts, then I suggest to download the source code of the extension and insert an additional event listener in the extension's source code.

  1. Get the extension's source (e.g. using the Chrome extension source viewer aka CRX Viewer).
  2. Unpack the zip file.
  3. Create a new HTML file, example_name.html, and let it contain:

    <script src="example_name.js"></script>
    
  4. Create a new script file, example_name.js, and let it call the extension's functionality, e.g.:

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

    chrome.exe --pack-extension=directorycontainingextension
    

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

    (Starting from ChromeDriver 2.11, you can just zip the extension instead of packing it as a CRX file, and hard-code the extension ID in the manifest file by setting the "key" attribute (this "key" attribute is also printed to the F12 console by the CRX Viewer).)

After modifying the extension, you will have an extension with the same functionality as the original one, plus an additional HTML page. This new HTML page will invoke the extension's functionality when it is opened.

After doing this, "running" the extension is as easy as opening chrome-extension://[EXTENSION ID HERE]/example_name.html in a new tab.

If you don't like these new tabs, then you could also use the chrome.webRequest or chrome.declarativeWebRequest API to intercept custom URLs and activate the desired functionality whenever a page requests this resource. Then, you can just put the URL in an <img> to activate the extension's functionality.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...