Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
159 views
by
I am using Selenium WebDriver to execute my code. I have a scenario where I need to check the country of the browser. Can you explain how to do that?

let suppose the browser country is English(US) then you will have the date formats like this MM/DD/YYYY but if the browser country will be Australia like English(Australia) then the date format will be this DD/MM/YYYY. Now I need to put the automation for this date validation method but first, I need to identify whether the browser has Australia or US

1 Answer

0 like 0 dislike
by Master (1.2k points)

You can do that by executing a JavaScript,

JavascriptExecutor js = (JavascriptExecutor)driver;

String response = (String) js.executeScript("return navigator.language || navigator.userLanguage;");

The above code will return the local language of the browser. From that, you can determine the country.

Further, if you want to check time zone, use this,

JavascriptExecutor js = (JavascriptExecutor)driver;

String response = (String) js.executeScript("return new Date().getTimezoneOffset();");

Hope that helps!
...