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?
我知道如何在selenium webdriver中加载chrome扩展。但是,我没有看到任何文章或博客描述我如何使用Selenium运行chrome扩展。我需要显式地让chrome扩展运行/使它执行它的功能来自selenium。例如,我想使用Selenium Webdriver使用这个扩展来清除Chrome浏览器的缓存。我可以先做吗?还是Selenium WebDriver只帮助我将扩展加载到浏览器实例并将其留在那里?
2 个解决方案
#1
7
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.
当一个Chrome扩展被激活时,它已经在“运行”(至少它的背景/事件页面)。没有API可编程地单击按钮。
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.
如果您想轻松地使用现有扩展的功能,那么我建议您下载扩展的源代码,并在扩展的源代码中插入一个额外的事件监听器。
- Get the extension's source (e.g. using the Chrome extension source viewer aka CRX Viewer).
- 获取扩展的源代码(例如使用Chrome扩展源代码查看器即CRX查看器)。
- Unpack the zip file.
- 解压zip文件。
-
Create a new HTML file,
example_name.html
, and let it contain:创建一个新的HTML文件example_name。html,并让它包含:
<script src="example_name.js"></script>
-
Create a new script file,
example_name.js
, and let it call the extension's functionality, e.g.:创建一个新的脚本文件example_name。让它调用扩展的功能,例如:
chrome.runtime.getBackgroundPage(function(bg) { // Relevant function at the background page. In your specific example: bg.clearCache(); });
- Add the previous HTML file to
web_accessible_resources
in the manifest file. - 将先前的HTML文件添加到清单文件中的web_accessible_resources。
-
Pack the extension again, e.g. using the GUI at
chrome://extensions
or using重新打包扩展,例如使用chrome://扩展或使用GUI
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]".在创建directorycontainingextension。crx,在Chrome中加载这个crx文件,了解扩展名ID,如果不知道如何在Chrome中加载crx文件,请访问https://robwu。nl/crxviewer/,选择crx文件,打开F12开发工具,并在“计算扩展ID:(此处的扩展ID)”中复制32个字符的字符串。
(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).)(从ChromeDriver 2.11开始,只需压缩扩展名,而不是将其打包为CRX文件,并通过设置“key”属性在manifest文件中硬编码扩展名ID(这个“key”属性也被CRX查看器打印到F12控制台)。)
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.
在修改扩展之后,您将拥有一个具有与原始扩展相同功能的扩展,以及一个附加的HTML页面。这个新的HTML页面将在打开扩展时调用它的功能。
After doing this, "running" the extension is as easy as opening chrome-extension://[EXTENSION ID HERE]/example_name.html
in a new tab.
这样做之后,“运行”扩展就像打开chromeextension://[这里的扩展ID]/example_name一样简单。在一个新的标签。
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.
如果你不喜欢这些新的标签,那么你也可以使用chrome。声明式webRequest API拦截自定义url并在页面请求该资源时激活所需的功能。然后,您可以将URL放在中,以激活扩展的功能。
#2
0
The above solution is technically sound seem to be complicated , so I thought another way to do it. Because many times I need a lot of things that are better done manually, authentications, certain cookies, etc
上面的解决方案在技术上听起来似乎很复杂,所以我想到了另一种方法。因为很多时候我需要很多更好的手工操作,认证,特定的cookie等等
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 do the logins I need now every-time I start the Webdriver with that folder everything is there
然后我手动安装扩展,并做我现在需要的登录,每次我用那个文件夹启动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
The advantage is you can use multiple folders with different setting and Extensions without the need to install and uninstall Extensions, change settings, change logins etc
好处是,您可以使用具有不同设置和扩展的多个文件夹,而不需要安装和卸载扩展、更改设置、更改登录等。
#1
7
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.
当一个Chrome扩展被激活时,它已经在“运行”(至少它的背景/事件页面)。没有API可编程地单击按钮。
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.
如果您想轻松地使用现有扩展的功能,那么我建议您下载扩展的源代码,并在扩展的源代码中插入一个额外的事件监听器。
- Get the extension's source (e.g. using the Chrome extension source viewer aka CRX Viewer).
- 获取扩展的源代码(例如使用Chrome扩展源代码查看器即CRX查看器)。
- Unpack the zip file.
- 解压zip文件。
-
Create a new HTML file,
example_name.html
, and let it contain:创建一个新的HTML文件example_name。html,并让它包含:
<script src="example_name.js"></script>
-
Create a new script file,
example_name.js
, and let it call the extension's functionality, e.g.:创建一个新的脚本文件example_name。让它调用扩展的功能,例如:
chrome.runtime.getBackgroundPage(function(bg) { // Relevant function at the background page. In your specific example: bg.clearCache(); });
- Add the previous HTML file to
web_accessible_resources
in the manifest file. - 将先前的HTML文件添加到清单文件中的web_accessible_resources。
-
Pack the extension again, e.g. using the GUI at
chrome://extensions
or using重新打包扩展,例如使用chrome://扩展或使用GUI
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]".在创建directorycontainingextension。crx,在Chrome中加载这个crx文件,了解扩展名ID,如果不知道如何在Chrome中加载crx文件,请访问https://robwu。nl/crxviewer/,选择crx文件,打开F12开发工具,并在“计算扩展ID:(此处的扩展ID)”中复制32个字符的字符串。
(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).)(从ChromeDriver 2.11开始,只需压缩扩展名,而不是将其打包为CRX文件,并通过设置“key”属性在manifest文件中硬编码扩展名ID(这个“key”属性也被CRX查看器打印到F12控制台)。)
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.
在修改扩展之后,您将拥有一个具有与原始扩展相同功能的扩展,以及一个附加的HTML页面。这个新的HTML页面将在打开扩展时调用它的功能。
After doing this, "running" the extension is as easy as opening chrome-extension://[EXTENSION ID HERE]/example_name.html
in a new tab.
这样做之后,“运行”扩展就像打开chromeextension://[这里的扩展ID]/example_name一样简单。在一个新的标签。
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.
如果你不喜欢这些新的标签,那么你也可以使用chrome。声明式webRequest API拦截自定义url并在页面请求该资源时激活所需的功能。然后,您可以将URL放在中,以激活扩展的功能。
#2
0
The above solution is technically sound seem to be complicated , so I thought another way to do it. Because many times I need a lot of things that are better done manually, authentications, certain cookies, etc
上面的解决方案在技术上听起来似乎很复杂,所以我想到了另一种方法。因为很多时候我需要很多更好的手工操作,认证,特定的cookie等等
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 do the logins I need now every-time I start the Webdriver with that folder everything is there
然后我手动安装扩展,并做我现在需要的登录,每次我用那个文件夹启动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
The advantage is you can use multiple folders with different setting and Extensions without the need to install and uninstall Extensions, change settings, change logins etc
好处是,您可以使用具有不同设置和扩展的多个文件夹,而不需要安装和卸载扩展、更改设置、更改登录等。