使用Selenium Python在Firefox上更改语言 [英] Change language on Firefox with Selenium Python

查看:477
本文介绍了使用Selenium Python在Firefox上更改语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to change the language of Selenium Webdriver Firefox from English to Spanish.

我有以下代码:

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

但是,返回的驾驶员仍然是英语,而不是西班牙语.我想念什么?如何将语言设置为西班牙语?

However, the driver being returned is still in English and not in Spanish. What am I missing? How can I set the language to Spanish?

推荐答案

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

To change the language for Firefox Browser exectued by Selenium do as follows:

英语:

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.

There is no need to import FirefoxProfile, because this method is linked to webdriver.

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

Here you'll find a full list of all country/language codes: https://de.wikipedia.org/wiki/Liste_der_ISO-639-1-Codes

这篇关于使用Selenium Python在Firefox上更改语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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