-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Description
What happened?
REPRODUCTION STEPS
- In the afternoon, using Selenium with any WebDriver, load any website that sets cookies that expire
- Print the output of the toString() method for the cookies.
EXPECTED RESULTS
Expiry date is printed in either 24-hour format or a 12-hour format with an AM/PM indicator
ACTUAL RESULTS
Expiry date is printed in a 12-hour format with no AM/PM indicator.
IMPACT
What this means is that an expiry time of "4:23:21 PM EDT" gets printed as "04:43:21 EDT" - implying "04:43:21 AM EDT".
This can make debugging interesting, and intermittently breaks use cases that use the toString() method to save cookies. But works fine in the morning. Doing a Google search for
Selenium Cookie.toString()
reveals many examples like this where that method is used to save cookies. This will result in valid cookies being marked as expired in the afternoon.
There are multiple workarounds
- Don't use toString(), build the string manually using the getters
- Use toJson() to save cookies
ROOT CAUSE/FIX
The toString() method of common/src/java/org/openqa/selenium/Cookie.java formats the expiry as "EEE, dd MMM yyyy hh:mm:ss z"
Least disruptive fix would likely be to switch the hour format from "hh" (Hour in am/pm (1-12)) to "HH" (Hour in day (0-23))
Fixed code would look like
: "; expires=" + new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z").format(expiry))
How can we reproduce the issue?
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.time.LocalDateTime;
public class CookieTest2 {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
String website = "https://www.whatarecookies.com/cookietest.asp";
driver.get(website);
System.out.println("Current System Time: " + LocalDateTime.now());
System.out.println(driver.manage().getCookies());
driver.quit();
}
}Relevant log output
> Task :CookieTest2.main()
Current System Time: 2024-05-10T16:23:25.297501900
[dta=vcount%3D0%2Cprev%3D1715372601980; expires=Fri, 10 May 2024 04:43:21 EDT; path=/; domain=www.whatarecookies.com; sameSite=None, _ga_6QRS5RXYHS=GS1.2.1715372602.1.0.1715372602.0.0.0; expires=Sun, 10 May 2026 04:23:22 EDT; path=/; domain=.whatarecookies.com; sameSite=None, ASPSESSIONIDQETBTRSA=IPDOBBMCMKEMPGBEFJCNILOG; path=/; domain=www.whatarecookies.com;secure;; sameSite=None, FCNEC=%5B%5B%22AKsRol8zQbsSlVT7FIYKPVyOkjeIWs0UmAoO3Vdw8_CYei6Os22DCy6l1F_-BWa-FgyFXgU1ahtGZYZvFc74ws1b60OpPER7La69NMg-TJw0YlQDKrxJIVxb_HoODUuS8bKNjaaf5jFUDZEXvYX86_u9A8gPixTHqg%3D%3D%22%5D%5D; expires=Sat, 10 May 2025 04:23:25 EDT; path=/; domain=.whatarecookies.com; sameSite=None, _gat=1; expires=Fri, 10 May 2024 04:24:22 EDT; path=/; domain=.whatarecookies.com; sameSite=None, __gpi=UID=00000e05819e8840:T=1715372603:RT=1715372603:S=ALNI_MYA2y9P0tO5Clam7UbMNoaPoyb_sw; expires=Wed, 04 Jun 2025 04:23:23 EDT; path=/; domain=.whatarecookies.com; sameSite=None, _ga=GA1.2.691575010.1715372602; expires=Sun, 10 May 2026 04:23:22 EDT; path=/; domain=.whatarecookies.com; sameSite=None, __eoi=ID=1a500bf16b1c5e8b:T=1715372603:RT=1715372603:S=AA-AfjbH5vPECkfZCyNOs1SCv0Lh; expires=Wed, 06 Nov 2024 03:23:23 EST; path=/; domain=.whatarecookies.com; sameSite=None, cookieconsent=2AR; expires=Fri, 09 May 2025 08:00:00 EDT; path=/; domain=www.whatarecookies.com; sameSite=None, __gads=ID=d9f42925dc75504e:T=1715372603:RT=1715372603:S=ALNI_MZxL317YiDN0mcUVRErzD95e4O15w; expires=Wed, 04 Jun 2025 04:23:23 EDT; path=/; domain=.whatarecookies.com; sameSite=None, _gid=GA1.2.227887166.1715372602; expires=Sat, 11 May 2024 04:23:22 EDT; path=/; domain=.whatarecookies.com; sameSite=None]Operating System
Windows 10
Selenium version
4.20.0
What are the browser(s) and version(s) where you see this issue?
Firefox 125.0.3, likely affects all browsers
What are the browser driver(s) and version(s) where you see this issue?
FirefoxDriver, likely affects all drivers as the cookie class is common to all of them. expiry was added to toString() method in 2.34.0
Are you using Selenium Grid?
No