使用Selenium WebDriver运行Chrome扩展程序

7

我知道如何在Selenium webdriver中加载Chrome扩展程序,但是我没有看到任何帖子/博客描述如何从Selenium中运行Chrome扩展程序。

我需要显式地运行Chrome扩展程序/使其在Selenium中执行其功能。例如,我想使用这个扩展程序通过Selenium Webdriver清除Chrome浏览器的缓存。

我能做到吗?或者Selenium WebDriver只会帮助我将扩展程序加载到浏览器实例中并将其留在那里?

3个回答

8

当 Chrome 扩展程序被激活时,它已经在后台事件页上“运行”了。没有 API 可以通过编程方式单击该按钮。

如果您想要使用现有扩展的功能并减少工作量,则建议下载扩展的源代码并在源代码中插入一个额外的事件监听器。

  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).)

修改扩展后,您将拥有一个具有与原始扩展相同功能的扩展,并且还会有一个额外的HTML页面。当打开这个新的HTML页面时,它将调用扩展的功能。
这样做之后,"运行"扩展就像在新标签页中打开chrome-extension://[扩展ID]/example_name.html那么简单。
如果您不喜欢这些新标签页,那么您也可以使用chrome.webRequestchrome.declarativeWebRequest API来拦截自定义URL并激活所需功能,只要页面请求此资源,就可以将URL放入<img>中以激活扩展的功能。

  1. 创建一个新的脚本文件,例如 example_name.js,并让它调用扩展功能。
我该如何找出任何给定插件的功能由哪个方法负责?我想使用“视频下载专业版”插件下载视频。
- HelloWorldNoMore

0
接受的解决方案可能在技术上是正确的,但似乎过于复杂,有很多缺点,安装和更新扩展或多个扩展都很困难。所以我想到了另一种方法。 因为很多时候我需要很多事情最好手动完成,比如认证、特定的cookies等。
我使用文件夹作为配置文件,我运行:
chrome_options = Options()
chrome_options.add_argument("user-data-dir=selenium") 
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("www.google.com")

然后我手动安装扩展程序,并在每次启动Webdriver时进行所需的登录,这样,只要我使用那个文件夹启动,一切都在那里。
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

优点是您可以使用多个具有不同设置和扩展的文件夹,而无需安装和卸载扩展、更改设置、更改登录等。

1
请注意 - 这将帮助您加载扩展程序,但您无法使用上述代码触发扩展程序(例如在特定页面上单击/打开)。 - supputuri
@supputuri 如果扩展需要用户交互,上述代码无法处理,它只是加载扩展/扩展程序。 - Eduard Florinescu

0
如果您想要点击扩展图标,请尝试以下解决方案,希望能对您有所帮助:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.postMessage('clicked_browser_action', '*')");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接