Krid
Krid

Reputation: 309

Selenium Webdriver - set preferred browser language DE

I have a problem setting the preferred (accepted language) within headless Chrome using Selenium Webdriver and Ruby. I use the following WebDriver settings:

Selenium::WebDriver::Chrome.driver_path = @config[<path to the Chrome Driver>]

options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
options.add_argument('--disable-translate')
options.add_argument("--lang=de")

The driver is then initialized with:

@selenium_driver = Selenium::WebDriver.for :chrome, options: options

Everything works fine but aont some pages, Chrome returns English content even when I navigate to the German page URL (e.g. page.de). In these cases, the Chrome driver returns the English content due to an internal forwarding to page.de/en. I do not specify the en path in my queried URL.

I have tried to set the language using the Webdriver preference:

options.add_preference('accept_languages', 'de')

instead of the add_argument but it doesn't change anything of the behavior.

Does anyone have an idea how to force a headless Chrome controlled by Selenium Webdriver within Ruby to request page content in a defined language or - not optimal but it might help as a workaround - to stop the forwarding?

Upvotes: 3

Views: 16664

Answers (7)

Gertjan Al
Gertjan Al

Reputation: 21

Note that the --lang=de-DE argument is recently renamed to --accept-lang=de-DE.

Upvotes: 0

Dilip Prakash
Dilip Prakash

Reputation: 1

chrome_options.add_argument("--lang=en")
 
chrome_options.add_argument("--enable-experimental-accessibility-language-detection-dynamic")

chrome_options.add_argument("--enable-experimental-accessibility-language-detection")

This should help to set language to English for chrome headless mode

Upvotes: 0

eCorners
eCorners

Reputation: 77

This prefs hash inside an options hash did the trick for me. It's at the end of the driven_by :selenium line.

(Inside test/application_syste_test_case.rb)

# frozen_string_literal: true

require 'test_helper'
require 'capybara/rails'

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  driven_by :selenium, using: :chrome, screen_size: [1400, 1400], options: { prefs: { 'intl.accept_languages' => 'de,de-DE;q=0.9,en;q=0.1' } }

# ...

2021-06-14 UPDATE:

The previous example produces this deprecation warning:

WARN Selenium [DEPRECATION] :prefs is deprecated. Use Selenium::WebDriver::Chrome::Options#add_preference instead.

IMO, the solution below is uglier, but I'm posting it for when it's fully deprecated and the original stops working.

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  driven_by(:selenium,
    using: :chrome,
    screen_size: [1400, 1400],
    options: {
      options: Selenium::WebDriver::Chrome::Options.new(
        prefs: { 'intl.accept_languages' => 'de,de-DE;q=0.9,en;q=0.1' }
      )
    },
  )

Upvotes: 1

Juan Jos&#233;
Juan Jos&#233;

Reputation: 59

For me works:

options = Selenium::WebDriver::Firefox::Options.new
options.add_preference("intl.accept_languages", 'de-DE')
Capybara::Selenium::Driver.new(app, browser: :firefox, options: options)

Upvotes: 1

Krid
Krid

Reputation: 309

I found a solution that works for me. As in many cases the problem was sitting in front of the screen and simply doesn't work precisely enough ;-)

Instead of using

options.add_argument("--lang=de")

you have to use

options.add_argument("--lang=de-DE")

When I use an IETF language tag the code I initially posted works as expected.

Upvotes: 6

Gsk
Gsk

Reputation: 2945

You should be able to solve your problem by adding an experimental option:

options.add_option('prefs', {'intl.accept_languages': 'en,en_US'})

I'm sure it works with Python, but I've not tried with Ruby: this approach is the correct one, not sure about the implementation.
You can find in this repository the code which handles your problem in Python code, and in this Q&A how to implement experimental_options in Ruby

Upvotes: 0

Chris
Chris

Reputation: 3865

I'am using this in my test_helper.rb Works fine for me.

Capybara.register_driver :selenium do |app|
  Chromedriver.set_version "2.36"

  desired_capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    'chromeOptions' => {
      'prefs' => {
        'intl.accept_languages' => 'en-US'
     },
     args: ['disable-gpu', 'headless']
   }
  )

  Capybara::Selenium::Driver.new(app, { browser: :chrome, desired_capabilities: desired_capabilities })
end

Capybara.javascript_driver = :chrome
Capybara.default_driver = :selenium

Upvotes: 1

Related Questions