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

来源:互联网 发布:卖家开通淘宝客条件 编辑:程序博客网 时间:2024/06/06 01:25

本人在学习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]


下面是我写的代码

//向浏览器添加cookiespublic 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指向要访问的数据库名scutcsstatic String url = "jdbc:mysql://192.168.1.14:3306/DZJY";// MySQL配置时的用户名static String user = "root"; // MySQL配置时的密码static String password = "efa803254d";//end



1 0
原创粉丝点击