使用硒加载铬延伸

问题描述 投票:8回答:6

我想要的只是从网上商店加载chrome扩展程序。我做了很多搜索来弄明白,但只知道我们可以从本地机器加载扩展。我真的很想知道selenium是否具有从网上商店或网址加载扩展程序的功能。

请让我知道我正在尝试使用硒?

selenium selenium-webdriver google-chrome-extension google-chrome-devtools
6个回答
14
投票

我不确定为什么你特别关注从Webstore下载然后安装到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并转到扳手 - >工具 - >扩展程序 - 单击“开发者”模式旁边的+以显示开发人员选项 - 单击“包扩展名...”并选择版本文件夹作为根目录。将私钥文件留空。这将在版本文件夹中创建.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


7
投票
  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"); } //这是扩展程序网址,或者您可以在chrome:// extensions /上找到ID,找到扩展程序并复制ID。但是,URL必须是扩展URL。

4
投票

如果有人在看,我用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(chrome_options=options) 
driver.get('http://www.google.com')

3
投票

不知道为什么,但有人删除了他们的答案,这是正确的。这是内容(来自@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


0
投票
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());

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

0
投票

上述解决方案虽然在技术上听起来并不总是按预期工作,但我还是想到了另一种方法。因为很多时候我需要手动,身份验证,某些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

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

© www.soinside.com 2019 - 2024. All rights reserved.