1

I'm trying to override the user agent string, but couldn't find the solution in the internet so far...

This is my script:

<?php
namespace Facebook\WebDriver;

use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;

require_once('vendor/autoload.php');

$host         = 'http://localhost:4444/wd/hub';
$capabilities = DesiredCapabilities::chrome();
$capabilities->setPlatform(WebDriverPlatform::WINDOWS);
$capabilities->setCapability('userAgent', 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36');
$capabilities->setCapability('user-agent', 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36');
$driver       = RemoteWebDriver::create($host, $capabilities, 5000);

$driver->get('http://localhost/browser-emu/testpage.php?bot=1234');

// wait until the page is loaded
$driver->wait()->until(
    WebDriverExpectedCondition::titleContains('register')
);

echo "User agent: " . $driver->findElement(WebDriverBy::cssSelector('#userAgent'))->getText();

$driver->quit();

The page itself is as simple as:

<html>
<body>
<h1>testpage...</h1>

<?php
printf("<div id='userAgent'>%s</div> \n", $_SERVER['HTTP_USER_AGENT']);
?>
</body>
</html>

Doesn't matter what i've already tried, it's always saying the user agent is Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.109 Safari/537.36.

Does anybody know how I can override that?

I'm using (on Ubuntu):

  • facebook/webdriver @ 1.4.1
  • chromedriver @ 2.30.477691
Dennis
  • 916
  • 1
  • 10
  • 20
  • Possible duplicate of [Change user agent for selenium driver](https://stackoverflow.com/questions/29916054/change-user-agent-for-selenium-driver) – Florent B. Jun 23 '17 at 16:07
  • saw that, but this is not working in php or at least I don't know which classes to instantiate to the point where the driver is being instantiated. If I follow the semantic as shown in python, I get a `call to private FirefoxDriver::__construct() from invalid context` error when trying to get the driver with the profile. – Dennis Jun 23 '17 at 16:51
  • As instructed by the other post, you simply need to provide the user agent as a command line argument. Have a look at the api to for method: https://facebook.github.io/php-webdriver/latest/Facebook/WebDriver/Chrome/ChromeOptions.html – Florent B. Jun 23 '17 at 17:12
  • How? And which other post? – Dennis Jun 23 '17 at 17:39

1 Answers1

8

I've found the solution in the official wiki of facebook/web-driver (firefox):

$host = 'http://localhost:4444/wd/hub';

$profile = new FirefoxProfile();
$profile->setPreference('general.useragent.override', 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36');

$caps = DesiredCapabilities::firefox();
$caps->setCapability(FirefoxDriver::PROFILE, $profile);

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

And for Chrome:

$options = new ChromeOptions();
$options->addArguments(array(
    '--user-agent=' . $userAgent
));

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

$driver = RemoteWebDriver::create($host, $caps);
Dennis
  • 916
  • 1
  • 10
  • 20