Chromedriver issue #4550 breaks the user agent for device emulation via device name

Updated . Posted . Visible to the public.

Newest versions of Chromedriver breaks the user agent for device emulation via device name. In previous versions the user agent of the emulated device was set. In the newest versions the user agent differs from the emulated device.

In Capybara an affected config looks like following:

Capybara.register_driver :mobile_selenium do |app|
  options = Selenium::WebDriver::Chrome::Options.new
  options.add_emulation(device_name: 'iPhone 6')
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end

Running page.evaluate_script('navigator.userAgent') has changed for Chromedriver version 116.0.5845.96 with the example above. The user agent is not set anymore.

Previous result: Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1
Current result: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/116.0.5845.110 Safari/537.36

Temporary fix

You can configure your device manually with the configs from the Chromium source code Show archive.org snapshot . It is not possible to use device_name with the user_agent option.

mobileEmulation: A dictionary with either a value for “deviceName,” or values for “deviceMetrics” and “userAgent.” Refer to Mobile Emulation for more information.
Source Show archive.org snapshot

options.add_emulation(
  device_metrics: { width: 375, height: 667, touch: false, mobile: true },
  user_agent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
)
Last edit
Michael Leimstädtner
License
Source code in this card is licensed under the MIT License.
Posted by Emanuel to makandra dev (2023-09-07 07:52)