Mink + PhantomJS:如何设置用户代理?

时间:2014-08-15 01:09:33

标签: selenium phantomjs behat mink

我正在使用带有貂皮的幻影:

default:
  extensions:
    Behat\MinkExtension\Extension:
      goutte: ~
      selenium2:
        browser: phantomjs
        wd_host: http://localhost:8643/wd/hub
        capabilities:
           webStorageEnabled: true

但我需要伪装成最新的镀铬物。我试过这个:

    /**
     * @BeforeStep
     */
    public function masqueradeAsLatestChrome(StepEvent $event)
    {
        $this->getSession()->setRequestHeader('User-Agent', 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36');
    }

但我得到了例外:

[Behat\Mink\Exception\UnsupportedDriverActionException]                                                
Exception has been thrown in "beforeStep" hook, defined in FeatureContext::masqueradeAsLatestChrome()  
Request header is not supported by Behat\Mink\Driver\Selenium2Driver

Chrome的版本并不重要,但Web应用程序必须认为它与最新版本的chrome进行对话。

4 个答案:

答案 0 :(得分:3)

  

Selenium不提供此功能,因为它不是a   用户可以做到。它建议你使用代理注入额外的   标题为浏览器生成的请求。

https://code.google.com/p/selenium/issues/detail?id=2047#c1

可悲的是......然而,PhantomJS确实提供了interface for setting the headers。最好的方法是使用它的REST API向它发送一个直接命令。还有一个很酷的PHP wrap library可以让它变得容易200倍。

答案 1 :(得分:3)

你应该使用由JuanFranciscoCalderónZumba制作的新Behat / Mink驱动程序 https://github.com/jcalderonzumba

以下是指向驱动程序https://github.com/jcalderonzumba/MinkPhantomJSDriver

的直接链接

此驱动程序允许您指定所需的请求标头 (它适用于Behat 3.0,但我认为它至少需要PHP 5.4)

答案 2 :(得分:0)

您可以通过

在selenium2driver中传递其他设置
extra_capabilities:

所以在你的情况下:

default:
  extensions:
    Behat\MinkExtension\Extension:
      goutte: ~
      selenium2:
        browser: phantomjs
        wd_host: http://localhost:8643/wd/hub
        capabilities:
           webStorageEnabled: true
           extra_capabilities:
             phantomjs.page.settings.userAgent: "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36"

答案 3 :(得分:0)

对于https://github.com/facebook/php-webdriver,您可以使用此案例:

$capabilities = [
            WebDriverCapabilityType::BROWSER_NAME => 'phantomjs',
            WebDriverCapabilityType::PLATFORM => 'ANY',
            WebDriverCapabilityType::ACCEPT_SSL_CERTS => false,
            WebDriverCapabilityType::JAVASCRIPT_ENABLED => true,
            'phantomjs.page.settings.userAgent' => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246",
    ];
相关问题