使用PHP Selenium禁用图像加载

时间:2019-05-10 06:54:36

标签: selenium-chromedriver facebook-php-webdriver php-webdriver

如何禁用ChromeOptions中的图片加载? (PHP库)

我尝试了以下操作,但不确定语法是否正确

$options = new ChromeOptions();

// disable images
$options->addArguments(array(
   "service_args=['--load-images=no']"
));

$caps = DesiredCapabilities::chrome();
$caps->setCapability(ChromeOptions::CAPABILITY, $options);

$driver = RemoteWebDriver::create($host, $caps);

2 个答案:

答案 0 :(得分:1)

留下以下更完整的示例供以后参考:

这是可行的:

        $capabilities = DesiredCapabilities::chrome();
        $capabilities->setCapability('acceptInsecureCerts', true);
        $capabilities->setCapability(ChromeOptions::CAPABILITY_W3C, [
            'args' => [
                '--blink-settings=imagesEnabled=false',
            ]
        ]);

这也是可行的:

        $options = new ChromeOptions();

        $options->addArguments(
            [
                '--blink-settings=imagesEnabled=false',
            ]
        );

        $result = DesiredCapabilities::chrome();
        
        $result->setCapability(
            ChromeOptions::CAPABILITY_W3C,
            $options->toArray() // Notice that ->toArray() is used
        );

以下内容无效:

       $options = new ChromeOptions();

        $options->addArguments(
            [
                '--blink-settings=imagesEnabled=false',
            ]
        );

        $result = DesiredCapabilities::chrome();

        $result->setCapability(
            ChromeOptions::CAPABILITY_W3C,
            $options // Notice that ->toArray() is NOT used
        );

答案 1 :(得分:0)

要禁用,请使用参数:--blink-settings=imagesEnabled=false

$options->addArguments(array(
    '--blink-settings=imagesEnabled=false'
));

https://github.com/facebook/php-webdriver/issues/641#issuecomment-512255496