Reputation: 65
I'm trying to detect the language & culture of the browser, i need it so could format the date string to input it to the date field. I'm using Chrome as browser, selenium python and Pycharm as IDE.
Upvotes: 2
Views: 2049
Reputation: 187
A javascript solution for getting the browsers language:
var lang = window.navigator.userLanguage || window.navigator.language;
console.log(lang); //example output: 'en-GB'
You could then use the language in combination with toLocalDateString() to get the correct date format, for example:
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
var lang = window.navigator.userLanguage || window.navigator.language;
alert(date.toLocaleDateString(lang));
You can execute javascript in selenium like so:
language = driver.execute_script("return window.navigator.userLanguage || window.navigator.language")
I hope this helps.
Upvotes: 2