【问题标题】:Custom profile for ChromeChrome 的自定义配置文件
【发布时间】:2023-03-21 09:17:01
【问题描述】:

环境:Mac OS X 10.8.3、Ruby 2.0.0p0、selenium-webdriver 2.32.1、ChromeDriver 26.0.1383.0。

我想更改默认浏览器语言。我正在测试网站是否正确检测到浏览器语言并以该语言显示页面。

我能够将 Firefox 语言设置为德语:

require "selenium-webdriver"

profile = Selenium::WebDriver::Firefox::Profile.new 
profile["intl.accept_languages"] = "de"

caps = Selenium::WebDriver::Remote::Capabilities.firefox(firefox_profile: profile) 
caps.platform = "Linux" 
caps.version = 20

driver = Selenium::WebDriver.for( 
:remote, 
url: "http://USERNAME:ACCESS-KEY@ondemand.saucelabs.com:80/wd/hub", 
desired_capabilities: caps)

driver.navigate.to "http://sandbox.translatewiki.net/"

我想使用 Chrome(和其他浏览器,如果可能的话)做同样的事情。

我尝试了几件事试图在 Chrome 中以德语打开页面,但每次页面都以英语而不是德语显示。

require "selenium-webdriver"

profile = Selenium::WebDriver::Chrome::Profile.new 
profile["intl.accept_languages"] = "de"

caps = Selenium::WebDriver::Remote::Capabilities.chrome(firefox_profile: profile) 
caps.platform = "Linux" 
caps.version = ""

driver = Selenium::WebDriver.for( 
:remote, 
url: "http://USERNAME:ACCESS-KEY@ondemand.saucelabs.com:80/wd/hub", 
desired_capabilities: caps)

driver.navigate.to "http://sandbox.translatewiki.net/"

如果我将firefox_profile: profile 更改为profile: profilechrome_profile: profile,则每次都会以英文(而不是德文)打开页面。

据我在API docs 中看到的,仅支持:firefox_profile

我可以在 local machine 上做到这一点,但在使用 Sauce Labs 时却不行。

【问题讨论】:

  • 那么,如果你将 :firefox_profile 传递给 Chrome 大写,它是否有效?
  • @p0deje:它不起作用。无论我做什么,Chrome 总是以英文而不是德文打开页面。
  • 您确定如果您在浏览器中设置了默认语言,您的页面会正确加载吗?
  • 扳手 -> 选项 -> 高级 -> 更改字体和语言设置 -> 语言 -> Google Chrome 语言(在下拉菜单中选择您的语言)并接受
  • 我明白了,问题是它是否可以在没有 Selenium 的情况下工作。如果您更改 chrome 中的语言并且它仍然无法正常工作,那么您也永远不会使 selenium 工作,因为它基本上就是这样做的。

标签: ruby google-chrome selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

现在你可以使用这个方法

  def launch_browser options={}

    language = options.fetch(:language, "en_US")
    url = options.fetch(:url, "www.google.com")

    prefs = {
        :intl => {
            :accept_languages => language
        }
    }
    browser = Watir::Browser.new :chrome, :prefs => prefs

    browser.goto url
  end

然后你只需要打电话

    launch_browser :language => "de"

【讨论】:

    【解决方案2】:

    这应该可行:

    require "selenium-webdriver"
    
    profile = Selenium::WebDriver::Chrome::Profile.new 
    profile["intl.accept_languages"] = "de"
    
    caps = Selenium::WebDriver::Remote::Capabilities.chrome(
      platform: "Linux", 
      version: "", 
      'chrome.profile' => profile.as_json['zip']
    )
    
    Selenium::WebDriver.for(:remote, 
      url: "http://...@ondemand.saucelabs.com:80/wd/hub", 
      desired_capabilities: caps
    )
    

    【讨论】:

    • 我知道只有 Jari 可以回答这个问题!
    • 有史以来最好的花费 500 声望。 :)
    • 这已被验证有效吗?它似乎与我提出的解决方案非常接近,如果一个有效而另一个无效,我会感到惊讶。
    • 有人知道如何使用配置文件启用 Flash 插件吗?
    • “chrome.profile”键对我不起作用,但按照此评论所述进行设置很好:bugs.chromium.org/p/chromedriver/issues/detail?id=338#c18
    【解决方案3】:

    哇,SauceLabs + Chrome + Selenium + Ruby 的文档非常不一致,有时甚至相互矛盾。不幸的是,我没有要测试的 SauceLabs 帐户,所以我所能做的就是给你一些建议。

    This documentation 表示 ChromeDriver 不支持自定义配置文件是一个已知问题。 This post 展示了如何为 Chrome 设置自定义配置文件。去图吧。

    为此设置配置文件或默认语言不是标准WebDriver wire protocol 的一部分,因此您可能不走运。

    一种解决方法是将浏览器设置为使用代理,并在代理中添加/替换代理中的 Accept-Language 标头。

    不过,通过 Selenium Ruby 代码,看起来那篇文章可能会有所进展,所以试试看吧:

    profile = Selenium::WebDriver::Chrome::Profile.new
    profile["intl.accept_languages"] = "de"
    
    caps = Selenium::WebDriver::Remote::Capabilities.chrome
    caps['chromeOptions'] = { 'profile'    => profile.as_json['zip'] }
    driver = Selenium::WebDriver.for( 
    :remote, 
    url: "http://USERNAME:ACCESS-KEY@ondemand.saucelabs.com:80/wd/hub", 
    desired_capabilities: caps)
    
    driver.navigate.to "http://sandbox.translatewiki.net/"
    

    编辑:似乎--lang- 开关不能满足您的要求,因此请忽略以下内容。我把它留在这里留给后代。

    这可能有效(忘记配置文件,使用命令行开关):

    caps = Selenium::WebDriver::Remote::Capabilities.chrome
    caps['chrome.switches'] = ['--lang-de']
    

    【讨论】:

    【解决方案4】:

    我在本地机器上看到了德语翻译:

    profile = Selenium::WebDriver::Chrome::Profile.new
    profile["intl.accept_languages"] = "de"
    @driver = Selenium::WebDriver.for :chrome, :profile => profile  
    @target = 'http://sandbox.translatewiki.net/'
    

    osx:10.7.5

    ruby 1.9.3p0(2011-10-30 修订版 33570)[x86_64-darwin11.4.2]

    【讨论】:

    • Rich,正如我问题中的最后一句所说,我可以在本地机器上完成(请注意我的博客文章的链接),但在使用 Sauce Labs 时却不行。
    猜你喜欢
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 2014-01-15
    • 2018-06-28
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多