selenium2java利用mysq解决向浏览器插入cookies时token过期问题

在使用selenium2java通过浏览器插入cookies进行模拟登录时遇到token过期问题。通过连接MySQL数据库获取最新token,每次插入cookies时确保使用最新token,从而有效解决过期问题。关键cookies为'MyName'和'User_token_Session'。代码实现中包含getNewToken方法和MySql类的公共部分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本人在学习selenium2java中通过浏览器插入cookies模拟用户登录的时候,发现一个问题,就是token值过期的问题,后来学习了selenium2java连接数据库后找到了一个更好的解决方案。每次插入cookies的时候总是从数据库拿到最新的token,这样就完美解决了过期的问题。

这个是我登录后从浏览器拿到的cookies:

[Automatic_login=18436035355%7Ce3ceb5881a0a1fdaad01296d7554868d%7CStudent; expires=星期二, 21 三月 2017 01:59:55 CST; path=/;
 domain=www.dz101.com, Hm_lvt_52b97b391587eb6d3e582caa097d6f91=1489471192; expires=星期三, 14 三月 2018 01:59:56 CST; path=/; 
 domain=.dz101.com, MyName=18436035355; expires=星期二, 21 三月 2017 01:59:54 CST; path=/; 
 domain=www.dz101.com, User_token_Session=f24f16d472b222271e6dcf27077231b9; expires=星期二, 21 三月 2017 01:59:54 CST; path=/; 
 domain=www.dz101.com, User_identity_Session=1; expires=星期二, 21 三月 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]

经过分析和尝试发现,其实只有插入MyName和User_token_Session这两项就可以了。其他具体做什么也说不好,如果有人知道留言告知一下。

下面是我成功插入后的cookies:

[Hm_lvt_52b97b391587eb6d3e582caa097d6f91=1489472871; expires=星期三, 14 三月 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]


下面是我写的代码

//向浏览器添加cookies
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();
//查看浏览器cookies
// Set<Cookie> cooies = driver.manage().getCookies();
// System.out.println(cooies);
}


下面是getNewToken(String mobile))方法:

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用来执行SQL语句
Statement statement = conn.createStatement();
// 要执行的SQL语句
String sql = "select * from users where mobile = "+ mobile;
            output(sql);
// 结果集
ResultSet rs = statement.executeQuery(sql);
            System.out.println("查询结果如下所示:");
            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;
            System.out.println("查询结果如下所示:");
            while(rs2.next()){
            token = rs2.getString(token);
            output(token);
            saveToFile(getNow() + token, "runlog.log", false);
            }
            conn.close();
            return token;
}


由于用到的mysql的内容增多,我新建了一个名字MySql的类,把sql方法的公共部分提取出来,下面是MySql类公共的部分:

//start 驱动程序名
static String driver = "com.mysql.jdbc.Driver";
// URL指向要访问的数据库名scutcs
static String url = "jdbc:mysql://192.168.1.14:3306/DZJY";
// MySQL配置时的用户名
static String user = "root"; 
// MySQL配置时的密码
static String password = "efa803254d";
//end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值