使用Selenium Python更改Firefox上的语言

时间:2015-09-22 22:51:39

标签: python selenium webdriver locale remotewebdriver

我正在尝试将Selenium Webdriver Firefox的语言从英语更改为西班牙语。

我有以下代码:

def get_webdriver(attempts=3, timeout=60):
  firefox_profile = webdriver.FirefoxProfile()
  firefox_profile.set_preference("intl.accept_languages", "es-es")

  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

但是,被退回的司机仍然是英文,而不是西班牙文。我错过了什么?如何将语言设置为西班牙语?

3 个答案:

答案 0 :(得分:2)

所以我想出了答案:

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

因此,无论何时调用此函数,都要确保将语言环境的参数传递给您想要的任何语言。

例如,德语:

get_webdriver(locale='de') 

享受!

答案 1 :(得分:0)

要更改Selenium执行的Firefox浏览器的语言,请执行以下操作:

英语:

from selenium import webdriver

profile = webdriver.FirefoxProfile()
profile.set_preference('intl.accept_languages', 'en-US, en')
driver = webdriver.Firefox(firefox_profile=profile)

德语

from selenium import webdriver

profile = webdriver.FirefoxProfile()
profile.set_preference('intl.accept_languages', 'de-DE, de')
driver = webdriver.Firefox(firefox_profile=profile)

由于此方法已链接到webdriver,因此无需导入FirefoxProfile。

在这里您会找到所有国家/语言代码的完整列表: https://de.wikipedia.org/wiki/Liste_der_ISO-639-1-Codes

答案 2 :(得分:-1)

我对Selenium了解不多,但从我的研究来看,你可能会使用错误的语言关键字。从这个链接

https://groups.google.com/forum/#!topic/nightwatchjs/qwtLPIAJa_c

它看起来应该是Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains" 而不是QASpanish。您是否检查过以确保使用正确的关键字?

相关问题