Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a timezone in Selenium Chromedriver?

I can't figure out how to set a timezone when using Chromedriver. Is there some ChromeOptions argument or something?

The issue is that when I go to some sites (for example, https://whoer.net), it shows the system time that is equal to the time set on Windows. And I want to be able to change the Chromedriver's timezone somehow to perform timezone dependent testing.

I tried to set some chrome options:

Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("args", Arrays.asList("--disable-system-timezone-automatic-detection", "--local-timezone"));
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

It doesn't work.

Tried to do some weird thing using Javascript:

((JavascriptExecutor) driver).executeScript("Date.prototype.getTime = function() { return 1 };");
   

It didn't help either.

EDIT:

Found this https://sqa.stackexchange.com/questions/8838/faking-system-time-date-with-selenium-webdriver

Tried to execute javascript on page with the code copied from TimeShift.js like this:

((JavascriptExecutor) driver).executeScript("/*code from TimeShift.js here*/ TimeShift.setTimezoneOffset(-60);");

System time at https://whoer.net didn't change. What am I doing wrong?

like image 286
Helen Avatar asked Jun 22 '17 00:06

Helen


People also ask

How do I check my browser time zone?

Open DevTools in Chrome -> Open the Console drawer. Click on the three-dotted menu -> Click on More tools -> Sensors. From the Sensors tab, set the location according to your preference and define the specific timezone. Refer to the image below to better understand how to set a timezone for testing in Chrome.

Where do I put Selenium in ChromeDriver?

Now we need to move ChromeDriver somewhere that Python and Selenium will be able to find it (a.k.a. in your PATH ). The easiest place to put it is in C:\Windows . So move it there!

How do I change ChromeDriver language?

You can do it by adding Chrome's command line switches "--lang". Basically, all you need is starting ChromeDriver with an ChromeOption argument --lang=es , see API for details. The following is a working example of C# code for how to start Chrome in Spanish using Selenium.


1 Answers

You can do it by using Chrome DevTools Protocol, and here is the python code:

driver = webdriver.Chrome()
tz_params = {'timezoneId': 'America/New_York'}
driver.execute_cdp_cmd('Emulation.setTimezoneOverride', tz_params)
like image 114
garrythehotdog Avatar answered Sep 19 '22 20:09

garrythehotdog