selenium change language browser chrome / firefox

后端 未结 4 1928
一个人的身影
一个人的身影 2021-02-09 13:05

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.

4条回答
  •  时光取名叫无心
    2021-02-09 13:45

    This code apply to the simplest use case with browser running on local machine.

    For Firefox:

    from selenium import webdriver
    
    browser_locale = 'fr'
    gecko_driver_path = 'geckodriver64.exe'
    
    profile = webdriver.FirefoxProfile()
    profile.set_preference('intl.accept_languages', browser_locale)
    
    browser = webdriver.Firefox(executable_path=gecko_driver_path,
                                firefox_profile=profile)
    browser.get('https://google.com/')
    

    For Chrome:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    browser_locale = 'fr'
    chrome_driver_path = 'chromedriver.exe'
    
    options = Options()
    options.add_argument("--lang={}".format(browser_locale))
    
    browser = webdriver.Chrome(executable_path=chrome_driver_path,
                               chrome_options=options)
    browser.get('https://google.com/')
    

提交回复
热议问题