Set Chrome's language using Selenium ChromeDriver

SeleniumAutomation TestingTesting Tools

We can set Chrome browser language using Selenium webdriver. Some applications are designed such that it can be redesigned to multiple languages without any modifications. This is known as internationalization.

In Selenium, we can modify the language preferences with the help of ChromeOptions class for the Chrome browser. We shall create an object of this class and apply addArguments method on it.

To modify the language to Spanish, we have to pass −−lang=es as a parameter to the addArguments method. This information is then made available to the webdriver object.

Syntax

ChromeOptions opt = new ChromeOptions();
opt.addArguments("−−lang=es");
WebDriver drv = new ChromeDriver(opt);

Example

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.util.concurrent.TimeUnit;
public class ChromeLangSet{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
      "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      //ChromeOptions object
      ChromeOptions opt = new ChromeOptions();
      //set language to Spanish
      opt.addArguments("−−lang=es");
      // configure options parameter to Chrome driver
      WebDriver driver = new ChromeDriver(opt);
      driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);
      driver.get("https://www.google.com/ ");
   }
}

Output

raja
Published on 02-Feb-2021 11:38:58
Advertisements