Selenium Webdriver-设置首选的浏览器语言DE [英] Selenium Webdriver - set preferred browser language DE

查看:476
本文介绍了Selenium Webdriver-设置首选的浏览器语言DE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Selenium Webdriver和Ruby在无头Chrome中设置首选语言(接受的语言)时出现问题.我使用以下WebDriver设置:

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

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

Everything works fine but at 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.

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

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

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

代替add_argument,但它不会改变任何行为.

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

有没有人知道如何在Ruby中强制由Selenium Webdriver控制的无头Chrome浏览器以定义的语言请求页面内容,或者-不是最佳方法,但可能可以作为一种解决方法-停止转发?

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?

任何帮助表示赞赏

最佳

克里德

推荐答案

我找到了适合我的解决方案.在许多情况下,问题出在屏幕前,根本无法满足您的要求;-)

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

代替使用

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

您必须使用

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

当我使用IETF语言标签时,我最初发布的代码可以按预期工作.

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

这篇关于Selenium Webdriver-设置首选的浏览器语言DE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆