Selenium Webdriver - set preferred browser language DE

前端 未结 4 1416
一生所求
一生所求 2021-01-21 19:45

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

Sele         


        
相关标签:
4条回答
  • 2021-01-21 20:28

    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

    0 讨论(0)
  • 2021-01-21 20:33

    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.

    0 讨论(0)
  • 2021-01-21 20:39

    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)
    
    0 讨论(0)
  • 2021-01-21 20:45

    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
    
    0 讨论(0)
提交回复
热议问题