selenium change language browser chrome / firefox

后端 未结 4 1914
一个人的身影
一个人的身影 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

    I have this java code please modify it in python

    Using Firefox Browser :

    FirefoxProfile profile = new FirefoxProfile();
    //setting the locale french : ‘fr’
    profile.setPreference(“intl.accept_languages”,”fr”);
    driver = new FirefoxDriver(profile);
    driver.get(“http://google.co.in);
    

    Using Chrome Browser :

    System.setProperty(“webdriver.chrome.driver”,”D:/DollarArchive/chromedriver.exe”);
    ChromeOptions options = new ChromeOptions();
    options.addArguments(“–lang= sl”);
    ChromeDriver driver = new ChromeDriver(options);
    driver.get(“http://google.co.in);
    

    In python set something like below

    For firefox

    driver.set_preference(“intl.accept_languages”,”fr”)
    

    For Chrome

    options.add_argument(“–lang= sl”)
    

    Hope it will help you :)

    0 讨论(0)
  • 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/')
    
    0 讨论(0)
  • 2021-02-09 13:59

    FIREFOX JAVA

    Java code for changing the language to English:

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

    CHROME JAVA

    Java code for changing the language to English:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("lang=en-GB");
    driver = new ChromeDriver(options);
    

    FIREFOX PYTHON

    options = Options()
    profile = webdriver.FirefoxProfile()
    profile.set_preference('intl.accept_languages', 'en-GB')
    browser = webdriver.Firefox(options=options,firefox_profile=profile)
    
    0 讨论(0)
  • 2021-02-09 14:03

    The answer is already available in one of the very recent post:
    Change language on Firefox with Selenium Python

    Here is the code:

    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
    
    0 讨论(0)
提交回复
热议问题