How to Dynamically Refresh Selenium Cookies with Database Tokens in Java
This guide explains how to solve Selenium‑Java cookie expiration by fetching the latest user token from a MySQL database and inserting only the essential MyName and User_token_Session cookies before refreshing the browser session.
While using Selenium with Java to simulate user login by inserting cookies, the author discovered that the token stored in the User_token_Session cookie frequently expires, causing login failures.
To eliminate the expiration problem, the author switched to retrieving a fresh token from a MySQL database each time a cookie is added. After analysis, only two cookies— MyName and User_token_Session —are required for a successful login.
[Automatic_login=18436035355%7Ce3ceb5881a0a1fdaad01296d7554868d%7CStudent; expires=Tue, 21 Mar 2017 01:59:55 CST; path=/; domain=www.dz101.com, Hm_lvt_52b97b391587eb6d3e582caa097d6f91=1489471192; expires=Wed, 14 Mar 2018 01:59:56 CST; path=/; domain=.dz101.com, MyName=18436035355; expires=Tue, 21 Mar 2017 01:59:54 CST; path=/; domain=www.dz101.com, User_token_Session=f24f16d472b222271e6dcf27077231b9; expires=Tue, 21 Mar 2017 01:59:54 CST; path=/; domain=www.dz101.com, User_identity_Session=1; expires=Tue, 21 Mar 2017 01:59:54 CST; path=/; domain=www.dz101.com, PHPSESSID=1s2uvdrj33d72qvj2qlqojhsl7; path=/; domain=www.dz101.com, Hm_lpvt_52b97b391587eb6d3e582caa097d6f91=1489471196; path=/; domain=.dz101.com]
After simplifying to the two necessary cookies, the resulting set looks like this:
[Hm_lvt_52b97b391587eb6d3e582caa097d6f91=1489472871; expires=Wed, 14 Mar 2018 02:27:53 CST; path=/; domain=.dz101.com, MyName=18436035355; path=/; domain=www.dz101.com, User_token_Session=f24f16d472b222271e6dcf27077231b9; path=/; domain=www.dz101.com, PHPSESSID=uahgb7ll1405h0p5jhloipt7a2; path=/; domain=www.dz101.com, Hm_lpvt_52b97b391587eb6d3e582caa097d6f91=1489472873; path=/; domain=.dz101.com]
The Java method that adds these cookies is:
public static void addCookies(WebDriver driver, String mobile) throws ClassNotFoundException, SQLException, IOException {
Cookie a = new Cookie("MyName", mobile);
Cookie b = new Cookie("User_token_Session", MySql.getNewToken(mobile));
driver.manage().addCookie(a);
driver.manage().addCookie(b);
driver.navigate().refresh();
// Optional: view cookies with driver.manage().getCookies();
}The helper getNewToken method connects to the database, finds the user ID for the given mobile number, then queries the users_token table for the most recent token and returns it:
public static String getNewToken(String mobile) throws ClassNotFoundException, SQLException, IOException {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if (!conn.isClosed()) System.out.println("Succeeded connecting to the Database!");
Statement statement = conn.createStatement();
String sql = "select * from users where mobile = " + mobile;
ResultSet rs = statement.executeQuery(sql);
String id = null;
while (rs.next()) {
id = rs.getString("id");
System.out.println(rs.getString("id") + "\t" + id);
}
rs.close();
String sql2 = "select * from users_token where uid = " + id + " ORDER BY create_time DESC LIMIT 1";
ResultSet rs2 = statement.executeQuery(sql2);
String token = null;
while (rs2.next()) {
token = rs2.getString("token");
output(token);
saveToFile(getNow() + token, "runlog.log", false);
}
conn.close();
return token;
}By calling addCookies(driver, mobile), Selenium injects the fresh token each run, ensuring the login session remains valid without manual cookie updates.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
