使用“createSeleniumClient”运行 Symfony Panther 时如何禁用 GUI?

问题描述 投票:0回答:1

我想运行 Symfony Panther selenium 实例而不显示 GUI。我目前无法实现这一目标。我已经通过 Client::createChromeClient 实现了这一点,但是我现在正在使用 createSeleniumClient,因为我现在正在使用 Selenium Grid

我正在使用在 Laravel 11 中创建的自定义 CLI 命令 BrowserScrape.php。

我已经尝试过了

PANTHER_NO_HEADLESS=false php artisan browser:scrape

上述命令继续运行显示 GUI 的实例。这里不考虑 PANTHER_NO_HEADLESS。

我已经评论了我想要应用于 selenium 实例的选项的代码。

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Panther\Client;
use Facebook\WebDriver\Remote\DesiredCapabilities;

class BrowserScrape extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'browser:scrape';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * @var Client
     */
    private Client $client;

    public function __construct()
    {
        parent::__construct();
        $capabilities = array( // See https://www.browserstack.com/docs/automate/capabilities
            "os"                       => "Windows",
            "os_version"               => "11",
            "browser"                  => "Chrome",
            "browser_version"          => "latest",
            "name"                     => "Test",
            "build"                    => "Build 1.0",
            "browserstack.debug"       => true,
            "browserstack.console"     => "info",
            "browserstack.networkLogs" => true,
            "disableCorsRestrictions"  => true,
            "wsLocalSupport"           => true,
            "geoLocation"              => "GB"
        );
        $caps = DesiredCapabilities::chrome();
        foreach ($capabilities as $key => $value) {
            $caps->setCapability($key, $value);
        }
        $this->client = Client::createSeleniumClient(
            'http://localhost:'.config('app.selenium_grid_port').'/wd/hub',
            $caps,
            null,
            // [
            //     '--disable-popup-blocking',
            //     '--disable-application-cache',
            //     '--disable-web-security',
            //     '--start-maximized',
            //     '--ignore-certificate-errors',
            //     '--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
            //     '--window-size=1200,1100',
            //     '--headless',
            //     '--disable-gpu',
            // ],
        );
    }

    /**
     * @return void
     */
    private function getInput() {
        $words = $this->ask('>>');
        $this->info($words);
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->client
            ->get('https://www.imdb.com/search/name/?birth_monthday=12-10');
        $crawler = $this->client->getCrawler();
        $preferences = $crawler->filterXPath('//button[@data-testid="accept-button"]');
        $preferences->click();
        $element = $crawler->filterXPath('//h3[text()="1. Kenneth Branagh"]');
        $element->click();
        $this->client->takeScreenshot($saveAs = 'screenshot.jpg');


        return 0;
    }
}

php laravel symfony selenium-webdriver symfony-panther
1个回答
0
投票

我通过阅读本文档使用所需功能找到了修复程序。

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Panther\Client;
use Facebook\WebDriver\Remote\DesiredCapabilities;

class BrowserScrape extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'browser:scrape';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * @var Client
     */
    private Client $client;

    public function __construct()
    {
        parent::__construct();
        $capabilities = array( // See https://www.browserstack.com/docs/automate/capabilities
            "os"                       => "Windows",
            "os_version"               => "11",
            "browser"                  => "Chrome",
            "browser_version"          => "latest",
            "name"                     => "Test",
            "build"                    => "Build 1.0",
            "browserstack.debug"       => true,
            "browserstack.console"     => "info",
            "browserstack.networkLogs" => true,
            "disableCorsRestrictions"  => true,
            "wsLocalSupport"           => true,
            "geoLocation"              => "GB",
            "goog:chromeOptions"       => [
                "args" => [
                    '--disable-popup-blocking',
                    '--disable-application-cache',
                    '--disable-web-security',
                    '--start-maximized',
                    '--ignore-certificate-errors',
                    '--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
                    '--window-size=1200,1100',
                    // '--headless',
                    // '--disable-gpu',
                ],
            ],
        );
        $caps = DesiredCapabilities::chrome();
        foreach ($capabilities as $key => $value) {
            $caps->setCapability($key, $value);
        }
        $this->client = Client::createSeleniumClient(
            'http://localhost:'.config('app.selenium_grid_port').'/wd/hub',
            $caps,
        );
    }

    /**
     * @return void
     */
    private function getInput() {
        $words = $this->ask('>>');
        $this->info($words);
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->client
            ->get('https://www.imdb.com/search/name/?birth_monthday=12-10');
        $crawler = $this->client->getCrawler();
        $preferences = $crawler->filterXPath('//button[@data-testid="accept-button"]');
        $preferences->click();
        $element = $crawler->filterXPath('//h3[text()="1. Kenneth Branagh"]');
        $element->click();
        $this->client->takeScreenshot($saveAs = 'screenshot.jpg');


        return 0;
    }
}

现在我可以运行 BrowserScrape.php 命令的多个实例,而无需显示 GUI。

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