SeleniumBase icon indicating copy to clipboard operation
SeleniumBase copied to clipboard

Using `locale_code` does not change the local date format

Open Abdelgha-4 opened this issue 5 months ago • 0 comments

Using:

from seleniumbase import SB

with SB(uc=True, incognito=True, locale_code="en_us") as sb:
    # Set timezone and geolocation
    sb.activate_cdp_mode("https://www.example.com", tzone="America/New_York", geoloc=(40.7128, -74.0060))

    # Now test JavaScript date formatting
    date_str = sb.execute_script("return new Date(2025, 5, 24).toLocaleDateString();")
    locale = sb.execute_script("return Intl.DateTimeFormat().resolvedOptions().locale;")
    print(f"Formatted date: {date_str}")
    print(f"Resolved JS locale: {locale}")

Outputs:

Formatted date: 24/06/2025
Resolved JS locale: fr

However using:

from seleniumbase import SB

with SB(uc=True, incognito=True) as sb:
    # Set timezone and geolocation
    sb.activate_cdp_mode("https://www.example.com", tzone="America/New_York", geoloc=(40.7128, -74.0060))

    # Force JavaScript locale override using CDP
    sb.execute_cdp_cmd("Emulation.setLocaleOverride", {"locale": "en-US"})

    # Now test JavaScript date formatting
    date_str = sb.execute_script("return new Date(2025, 5, 24).toLocaleDateString();")
    locale = sb.execute_script("return Intl.DateTimeFormat().resolvedOptions().locale;")
    print(f"Formatted date: {date_str}")
    print(f"Resolved JS locale: {locale}")

Correctly outputs:

Formatted date: 6/24/2025
Resolved JS locale: en-US

Used version: 4.39.1

Abdelgha-4 avatar Jun 01 '25 19:06 Abdelgha-4