ruby - Selenium Webdriver - 设置首选浏览器语言 DE

标签 ruby selenium selenium-webdriver webdriver

我在使用 Selenium Webdriver 和 Ruby 的 headless Chrome 中设置首选(接受的语言)时遇到问题。我使用以下 WebDriver 设置:

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")

然后驱动程序初始化为:

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

一切正常,但在某些页面上,即使我导航到德语页面 URL(例如 page.de),Chrome 也会返回英语内容。在这些情况下,由于内部转发到 page.de/en,Chrome 驱动程序会返回英文内容。我没有在查询的 URL 中指定 en 路径。

我尝试使用 Webdriver 首选项设置语言:

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

而不是 add_argument 但它不会改变任何行为。

有没有人知道如何强制由 Ruby 中的 Selenium Webdriver 控制的 headless Chrome 以定义的语言请求页面内容,或者 - 不是最佳但它可能有助于解决问题 - 停止转发?

非常感谢任何帮助

最好的

克里德

最佳答案

我找到了适合我的解决方案。在许多情况下,问题出在屏幕前,根本无法准确工作 ;-)

而不是使用

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

你必须使用

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

当我使用 IETF 语言标记时,我最初发布的代码按预期工作。

关于ruby - Selenium Webdriver - 设置首选浏览器语言 DE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52098821/

相关文章:

ruby-on-rails - '.find' 和 '.where' 方法之间的性能差异

python - 如何通过conda更新selenium到3.9

python - 我们如何使用本地 Chrome 的 cookie 来使用 Selenium 登录?

java - 无法在 ExtentReport 中添加两个类的结果

python - BlazeDS Livecycle Data Services 有什么作用,而 PyAMF 或 RubyAMF 之类的东西不能做什么?

ruby - 推荐的 Ruby 交互式控制台

ruby-on-rails - Ruby on Rails 5.1.5 - 以下 TutorialsPoint 教程中的迁移错误(关系已存在)

python - 包装 Selenium "Expected Conditions"Python

linux - 在 Linux 服务器上运行 selenium 脚本时遇到 Chrome 驱动程序问题

java - 如何使用Selenium WebDriver清除JS输入的值?