selenium 更改语言浏览器 chrome/firefox [英] selenium change language browser chrome / firefox

查看:130
本文介绍了selenium 更改语言浏览器 chrome/firefox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 selenium 测试我的网站,但我无法更改浏览器的语言.我尝试使用 Firefox,也更改了配置文件,但它不起作用.

I'm trying to test my website with selenium but I don't manage to change the language of the browser. I tried with firefox, changing the profile also but it's not working.

很遗憾,因为我的许多内容都在更改语言.

That's a pity because many of my content is changing regarding the language.

这是我的python代码:

Here is my python code :

@classmethod
def setUpClass(cls):
    super(SeleniumTestCase, cls).setUpClass()
    options = Options()
    options.add_argument('--lang=en')
    cls.selenium = WebDriver(chrome_options=options)

所以通常我会更改语言但没有任何反应..

So normally I change the language but nothing happen..

提前致谢!

只是为了澄清.我已经检查过 stackoverflow,如果我发布这个问题,那真的是因为我尝试了我看到的大多数解决方案.

Just to clarify. I already checked on stackoverflow and if I post this question it's really because I tried most of the solutions I saw.

推荐答案

答案已经在最近的一篇博文中提供:
使用 Selenium Python 更改 Firefox 上的语言

The answer is already available in one of the very recent post:
Change language on Firefox with Selenium Python

代码如下:

def get_webdriver(attempts=3, timeout=60, locale='en-us'):
  firefox_profile = webdriver.FirefoxProfile()
  firefox_profile.set_preference("intl.accept_languages", locale)
  firefox_profile.update_preferences()

  desired_capabilities = getattr(
      DesiredCapabilities, "FIREFOX").copy()

  hub_url = urljoin('http://hub:4444', '/wd/hub')
  driver = webdriver.Remote(
    command_executor=hub_url, desired_capabilities=desired_capabilities,
    browser_profile=firefox_profile)

  return driver

这篇关于selenium 更改语言浏览器 chrome/firefox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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