从 Selenium WebDriver 运行 chrome 扩展 [英] Running a chrome extension from Selenium WebDriver

查看:58
本文介绍了从 Selenium WebDriver 运行 chrome 扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在 selenium webdriver 中加载 chrome 扩展.但是我没有看到任何描述我如何运行来自 Selenium 的 chrome 扩展的帖子/博客.

我需要明确地让 chrome 扩展运行/让它从 selenium 执行它的功能.例如,我想使用这个 扩展与Selenium Webdriver.

我可以先做吗?还是 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?

推荐答案

当 Chrome 扩展程序被激活时,它已经在运行"(至少它的背景/事件页面).没有用于以编程方式点击按钮的 API.

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. 获取扩展程序的源代码(例如,使用 Chrome 扩展程序源代码查看器 akaCRX 查看器).
  2. 解压 zip 文件.
  3. 创建一个新的 HTML 文件 example_name.html,并让它包含:

  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>

  • 创建一个新的脚本文件,example_name.js,并让它调用扩展的功能,例如:

  • 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();
    });
    

  • 将之前的 HTML 文件添加到清单文件中的web_accessible_resources.莉>
  • 再次打包扩展,例如使用 chrome://extensions 上的 GUI 或使用

  • Add the previous HTML file to web_accessible_resources in the manifest file.
  • Pack the extension again, e.g. using the GUI at chrome://extensions or using

    chrome.exe --pack-extension=directorycontainingextension
    

    创建directory containsextension.crx 后,在Chrome 中加载这个crx 文件以知道扩展ID.如果您不知道如何在 Chrome 中加载 crx 文件,只需访问 https://robwu.nl/crxviewer/,选择crx 文件,打开 F12 开发者工具并复制计算的扩展 ID:[此处的扩展 ID]"处的 32 个字符的字符串.

    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]".

    (从 ChromeDriver 2.11 开始,您可以压缩扩展程序而不是打包它作为 CRX 文件,并通过设置 "key" 属性在清单文件中硬编码扩展 ID(此 "key" 属性也打印到 F12CRX 查看器的控制台).)

    (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 页面将在打开时调用扩展程序的功能.

    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.

    执行此操作后,运行"扩展程序就像在新选项卡中打开 chrome-extension://[EXTENSION ID HERE]/example_name.html 一样简单.

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

    如果您不喜欢这些新标签,那么您也可以使用 chrome.webRequestchrome.declarativeWebRequest API 来拦截自定义 URL 并在任何时候激活所需的功能页面请求此资源.然后,您只需将 URL 放在 中即可激活扩展程序的功能.

    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.

    这篇关于从 Selenium WebDriver 运行 chrome 扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆