【问题标题】:Load chrome extension using selenium使用 selenium 加载 chrome 扩展
【发布时间】:2016-03-17 07:11:49
【问题描述】:

在运行 selenium 时,我需要从网上商店加载一个 chrome 扩展。 在我的研究中,我只发现了如何从本地机器加载扩展。

selenium 是否可以从 Web Store 加载扩展程序?

【问题讨论】:

  • 一个选项是每次运行测试脚本时使用 java 从 webstore 下载 crx 文件,然后将下载的脚本加载到 chromeoptions。有许多选项可以用 Java 从 Internet 下载文件。您的情况可以接受这种方法吗?
  • 是的,即使从商店下载扩展程序然后安装它也可以。但我不确定我们是否可以下载 CRX 文件?我们有可用的下载 URL 吗?
  • 是的,可以使用 selenium。参考这个Installing extensions via ChromeDriver in selenium
  • 我没有看到任何提议的解决方案提供了一种无需人工交互即可实现此目的的方法 - 即手动加载 crx 文件。除了我的以外,我的都提供了以编程方式下载 crx 文件的能力。我们不是在谈论在启动 chrome 之前使用 load-extension 或 add.argument 选项。 nuff 说。

标签: selenium selenium-webdriver google-chrome-extension google-chrome-devtools


【解决方案1】:

我不知道你为什么特别喜欢从网上商店下载然后安装到 Chrome 中。

我找到了一些下载 chrome 扩展的步骤:

-在电脑联网的情况下,从扩展页面安装扩展:https://chrome.google.com/webstore/detail/
-导航到扩展源代码。在 XP 中,可以在以下位置找到:C:\Documents and Settings\\Local Settings\Application Data\Google\Chrome\User Data\Default\Extensions\
- 您应该看到一个版本文件夹(即“0.0.21_0”)。复制此文件夹并将其移动到您要安装的机器上。
- 在断开连接的机器上打开 chrome 并转到 Wrench -> Tools -> Extensions
- 点击开发者模式旁边的 + 以显示开发者选项
-单击“打包扩展...”并选择版本文件夹作为根目录。将私钥文件留空。这将在版本文件夹中创建一个 .crx 文件以及一个私钥,就像您是开发人员一样。

--或者--

1- 找到您感兴趣的扩展程序的 ID。在扩展程序的详细信息页面上,它将类似于: bfbmjmiodbnnpllbbbfblcplfjjepjdn 后 https://chrome.google.com/webstore/detail/

2- 将其粘贴到任何其他浏览器(不是 Chrome): https://clients2.google.com/service/update2/crx?response=redirect&x=id%3D~~~~%26uc

3- 并将 ~~~~ 替换为扩展 ID。 系统将提示您保存 CRX 文件。将此文件拖到 Chrome 窗口并继续安装。

来源:https://productforums.google.com/forum/#!topic/chrome/g02KlhK12fU

最后,在 ChromeOptions 中使用下载的 .crx 文件来加载扩展

ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);

来源:https://sites.google.com/a/chromium.org/chromedriver/extensions

【讨论】:

  • 我使用 JS 执行此操作,它返回给定 ID 的下载 URL。我的最终目标是拦截来自差异扩展的请求。为此,我正在使用我的另一个扩展,我正在使用“chrome.webRequest.onBeforeRequest”API 来捕获请求。但似乎 chrome 不会将来自扩展的请求转发给我的处理程序。我可以使用的另一个 API 是“chrome.devtools.network.onRequestFinished”,但它仅在打开开发人员工具时才有效。现在我被卡住了如何使用 Selenium 打开开发者工具?能否请您分享我如何跟踪来自不同扩展程序的请求?
  • 不太熟悉 chrome 扩展开发。请参阅此帖子 stackoverflow.com/questions/23825371/… 关于修改扩展以在 javascript 中包含用户所需的操作。不知道会不会有帮助
  • 回到 2017 年,似乎链接方法不再有效。谁能确认一下?
  • @firechant 请参阅 stackoverflow.com/a/14099762 了解更新的方法
【解决方案2】:

我用 Python 来做这件事,以防有人在看。

您所要做的就是下载 .crx 文件(我使用 https://chrome-extension-downloader.com/)并将其保存在 Python 可以访问的位置。在我的示例中,我将它导入到与我的 Python 脚本相同的文件夹中,以加载 exampleOfExtensionDownloadedToFolder.crx。

from selenium import webdriver 
from selenium.webdriver.chrome.options import Options 

options = webdriver.ChromeOptions()
options.add_extension('./exampleOfExtensionDownloadedToFolder.crx')
driver = webdriver.Chrome(options=options) 
driver.get('http://www.google.com')

【讨论】:

  • 任何人获得CRX验证失败,这篇文章帮助stackoverflow.com/a/58899807/1577947
  • 这也适用于自定义打包扩展的 zip
  • "CRX验证解析扩展头失败,Chrome只支持CRX3格式,扩展需要更新吗?"
【解决方案3】:

以下是如何将 chrome 扩展加载到 chrome Selenium Python 中

日期 = 20-12-19
Chrome 版本= 79.0.3945.88

新版本的 Chrome 支持 crx.crx (crx3),如果你使用 crx 会报错。
如果你使用的是 73 或更高版本的 chrome,那么只需按照此步骤操作


1> 创建 crx3 文件。

1.去Chrome网上商店搜索你的扩展,复制扩展的链接。 Screen shot
2. 转到此site 并粘贴链接并为您的 Chrome 扩展程序下载 crx 文件。
3. 前往this GitHub页面下载模块,将您的crx文件转换为crx3或crx.crx。
4. 现在你有了 crx.crx 或 (crx3) 文件


**2> 在 selenium 中添加 chrome 扩展的 Python 代码**

1. 将您的 extension.crx.crx 文件与您的代码放在同一文件夹中或给出路径
2。您可以复制粘贴此代码,只需在 `chrome_options.add_extension(' YOUR - EXTENSION - NAME ')` 处更改文件 crx.crx 名称
import os
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    
    executable_path = "/webdrivers"
    os.environ["webdriver.chrome.driver"] = executable_path
    
    chrome_options = Options()

    chrome_options.add_extension('  YOUR - EXTIONTION  - NAME    ')
    
    driver = webdriver.Chrome(chrome_options=chrome_options)
    driver.get("http://stackoverflow.com")

【讨论】:

  • 这是一个很好的答案,应该会在 2020 年被接受。非常感谢!
  • @Khan Saad 您能否帮我指出任何博客文章或文档,说明如何在成功添加到驱动程序后执行扩展?
  • Github 不转换 crx3 中的 crx,它转换 cr3 中的文件夹需要更多说明
  • 但最终它完成了这项工作,那么重要吗?
【解决方案4】:
  1. 如果您想按照此操作并获得成功的结果,请将 chromedriver exe 放入您的文档文件中。

  2. 从 Google 下载“GET CRX”扩展。

  3. 下载您的扩展程序(即我的扩展程序是“DHS”用于 Rest API 测试)。

  4. 转到 Chrome 网上应用店 >> 搜索您的扩展程序(您已经下载的那个) >> 右键单击​​它并单击 :: GET CRX
    (这应该下载 CRX 文件。对于我来说,CRX 文件是“extension_1_2_5.crx”)

  5. 将 CRX 文件拖放到任何 Chrome 窗口中(这可以拒绝它但不用担心)。

  6. 现在,构建您的测试并执行

    public static void openChromeExtension(){
    
        System.setProperty("webdriver.chrome.driver", "/Users/[your local name]/Documents/chromedriver");
    
        ChromeOptions options = new ChromeOptions();
        options.addExtensions(new File("/Users/[your local name]/Documents/extension_1_2_5.crx"));
    
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        ChromeDriver driver = new ChromeDriver(capabilities);
        System.out.println("Opening extension");
        driver.get("chrome-extension://**aejoelaoggembcahagimdiliamlcdmfm**/dhc.html"); 
    
        driver.navigate().refresh();
        System.out.println("Refresh successfully");
    }
    

    //这是扩展的 URL,或者您可以在 chrome://extensions/ 上获取 id,找到扩展并复制 ID。但是,该 URL 必须是扩展 URL。

【讨论】:

  • 它给我的文件是 .bin 文件而不是 .crx 文件。这正常吗?
  • "CRX验证解析扩展头失败,Chrome只支持CRX3格式,扩展需要更新吗?"
【解决方案5】:

不知道为什么,但有人删除了他们的答案,这是正确的。以下是内容(来自@parishodak):

ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);

这个特殊的例子是用 Java 编写的

【讨论】:

  • 我使用上面的代码在 Selenium WD java 中加载了 chrome 扩展 - Google Tag Assistant。如何打开/观察 Tag Assistant 面板?
【解决方案6】:

上述解决方案虽然在技术上听起来并不总是按预期工作,所以我想了另一种方法来做到这一点。 因为很多时候我需要很多最好手动完成的事情、身份验证、某些 cookie 等

我使用文件夹作为配置文件,我运行:

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

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

【讨论】:

    【解决方案7】:
    using System.IO;
    using System.IO.Compression;
    
    
    
      public static class ChromeExtension
            {
                public static string Execute()
                {
                    var ParentPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent;
                    var DirectoryPath = ParentPath.FullName.Remove(ParentPath.FullName.IndexOf(ParentPath.Name));
    
                    string startPath = $"{DirectoryPath}\\Exchanger\\ChromeExtension";
                    string zipPath = $"{DirectoryPath}Exchanger\\Extension.zip";
    
                    if (System.IO.File.Exists(zipPath))
                    {
                        System.IO.File.Delete(startPath);
                    }
    
                    ZipFile.CreateFromDirectory(startPath, zipPath);
    
    
                    if (System.IO.File.Exists($"{DirectoryPath}\\Exchanger\\Extension.crx"))
                    {
                        System.IO.File.Delete($"{DirectoryPath}\\Exchanger\\Extension.crx");
                    }
    
                    System.IO.File.Move(zipPath, $"{DirectoryPath}\\Exchanger\\Extension.crx");
    
                    return $"{DirectoryPath}\\Exchanger\\Extension.crx";
                }
    
            }
    

    ....////....

    Used: 
    var options = new ChromeOptions();   
    options.AddExtension(ChromeExtension.Execute());
    
    ....////....
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-20
      • 1970-01-01
      • 1970-01-01
      • 2020-10-02
      • 2016-12-06
      • 2022-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多