【问题标题】:Change language on Firefox with Selenium Python使用 Selenium Python 在 Firefox 上更改语言
【发布时间】:2015-09-22 22:51:39
【问题描述】:

我正在尝试将 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

但是,返回的驱动程序仍然是英语而不是西班牙语。我错过了什么?如何将语言设置为西班牙语?

【问题讨论】:

  • 你怎么知道用错了?你试过es吗?
  • 请看我在下面发布的答案。谢谢!

标签: python selenium webdriver locale remotewebdriver


【解决方案1】:

要更改 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)

FirefoxProfile 不需要导入,因为这个方法是链接到 webdriver 的。

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

【讨论】:

    【解决方案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') 
    

    享受吧!

    【讨论】:

      【解决方案3】:

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

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

      看起来应该是QASpanish 而不是es-es。您是否检查过以确保您使用了正确的关键字?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-06
        • 2019-02-17
        • 2018-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多