user3049523
user3049523

Reputation: 65

Detect the language of the browser in selenium python?

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.

Here screen capture of the date field formats

Upvotes: 2

Views: 2049

Answers (1)

jbob77435
jbob77435

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

Related Questions