selenium 更改语言浏览器 chrome / firefox

Das*_*yth 6 python testing selenium

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

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

这是我的python代码:

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

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

提前致谢 !

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

Nor*_*yan 11

火狐浏览器

将语言更改为英语的Java代码:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("intl.accept_languages", "en-GB");
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
driver = new FirefoxDriver(options);
Run Code Online (Sandbox Code Playgroud)

爪哇

将语言更改为英语的Java代码:

ChromeOptions options = new ChromeOptions();
options.addArguments("lang=en-GB");
driver = new ChromeDriver(options);
Run Code Online (Sandbox Code Playgroud)

火狐Python

options = Options()
options.set_preference('intl.accept_languages', 'en-GB')
browser = webdriver.Firefox(options=options)
Run Code Online (Sandbox Code Playgroud)


Pap*_*Jha 4

答案已经在最近的一篇文章中提供:
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
Run Code Online (Sandbox Code Playgroud)