【问题标题】:Laravel Dusk lode Chrome extensionsLaravel Dusk lode Chrome 扩展
【发布时间】:2020-05-24 07:42:03
【问题描述】:

我正在使用 Laravel Dusk 使用我的浏览器自动执行程序 Laravel Dusk 使用 ChromeDriver 实现自动化 我遇到的问题是我在常规 Google Chrome 上安装了一些扩展程序

但是,当我通过 Laravel Dusk 加载 ChromeDriver 时,没有任何扩展加载它。

有没有办法让它们加载?

【问题讨论】:

    标签: laravel laravel-dusk


    【解决方案1】:

    想知道该怎么做,这里有一个使用 MetaMask 扩展作为示例的解决方案:

    首先需要编辑 DuskTestCase.php 文件的驱动函数:

    /**
     * Create the RemoteWebDriver instance.
     *
     * Get "user-data-dir" from Google Chrome Profile Path parameters: chrome://version/
     *
     * @return \Facebook\WebDriver\Remote\RemoteWebDriver
     */
    protected function driver()
    {
        // --user-data-dir & --profile-directory are optional
        $options = (new ChromeOptions)->addArguments(collect([
            '--window-size=1920,1080',
            //'--user-data-dir='.$_ENV["GOOGLE_CHROME_USER_DATA_DIR"],
            // '--profile-directory='.$_ENV["GOOGLE_CHROME_USER_PROFILE"],
        ])->unless($this->hasHeadlessDisabled(), function ($items) {
            return $items->merge([
               //'--disable-gpu',
               //'--headless',
            ]);
        })->all());
    
        // Here you add an array with the paths of the extensions you want to add, must be files in .CRX format
        try {
            $options->addExtensions([$_ENV['METAMASK_EXTENSION_PATH']]);
        } catch (\Throwable $e) {
            //
        }
    
        return RemoteWebDriver::create(
            $_ENV['DUSK_DRIVER_URL'] ?? 'http://localhost:9515',
            DesiredCapabilities::chrome()->setCapability(
                ChromeOptions::CAPABILITY, $options
            )
        );
    }
    

    代码'$options->addExtensions([$_ENV['METAMASK_EXTENSION_PATH']]);'以 CRX 格式指定扩展文件的路径数组。

    因此,您需要将扩展​​名放在您选择的目录中的 .crx 中。

    要将 Google Chrome 扩展程序转换为 CRX 格式,步骤如下:(来源:https://dev.to/ltmenezes/automated-dapps-scrapping-with-selenium-and-metamask-2ae9

    • 在常规 Chrome 上安装 Metamask(或您选择的扩展程序)
    • 导航到 chrome://extensions/
    • 单击“打包扩展”并输入 Metamask 扩展的本地路径。 Mac Os X PATH_TO/Library/Application Support/Google/Chrome/Default/Extensions/EXTENSION_IDENTIFIER/FOLDER/ 中的某处

    见:Where does Chrome store extensions?

    这将生成一个 .crx 文件,您可以使用该文件作为 Chromium 上的扩展程序加载。

    如果扩展程序第一次需要大量配置,最好将其安装在新的 Google Chrome 配置文件中,配置它,然后提供 --user-data-dir 和 --profile-directory 作为 ChromeOptions 到加载它。在这种特定情况下,扩展程序已经安装,因此带有 '$options->addExtensions([$_ENV['METAMASK_EXTENSION_PATH']]);' 的部分可以评论。

    # Google CRX3 from extension to download a Chrome extension in .crx format
    METAMASK_EXTENSION_PATH=/CHANGE_ME/metamask-chrome-10.8.1.crx
    
    # See Profile Path in Google Chrome with chrome://version/
    # The last part of the full Profile Path is the User Profile name to set for GOOGLE_CHROME_USER_PROFILE,
    # remove that last part to set GOOGLE_CHROME_USER_DATA_DIR
    GOOGLE_CHROME_USER_DATA_DIR="/CHANGE_ME/Library/Application Support/Google/Chrome/"
    GOOGLE_CHROME_USER_PROFILE=Default
    

    【讨论】:

      猜你喜欢
      • 2019-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多