admin管理员组

文章数量:1656451

系列文章目录

第一篇:Java实现QQ登录


文章目录

  • 系列文章目录
  • 前言
  • 登录
    • selenium登录
    • 登录流程
    • selenium操作
    • 获取登录数据
    • 完整代码
  • 总结


前言

本文内容仅供学习参考,禁止商业用途

上篇文章中主要介绍如何利用java,通过模拟http的形式登录QQ,但是这种方式会因为tx更新密码加密算法而失效,所以本篇文章中会介绍如何通过Selenium来实现QQ的自动化登录


经过本人分析,因为浏览器在发送login之后,response body中并没有返回,而是将一系列登录信息都存放在cookie里了,那么就带来了另外一种登录方式。

登录

selenium登录

selenium可以实现自动的操作,就像人在浏览器中操作一样,而且通过配置,可以得到当前网页中的cookie和操作日志。

登录流程

详细登录流程见https://blog.csdn/majixiang1996/article/details/112651478

同样本文以登录https://lol.qq为例

在本文描述的登录方式中,只需要

  • 打开登录页
  • 点击切换到密码登录
  • 输入账号密码
  • 点击登录按钮
  • 获取cookie

这五个步骤,简单快捷稳定,只需要qq登录页没有大的改动的话,就不需要做改动。

selenium操作

  • 打开登录页
  // 登录主页
  driver.get("https://lol.qq/space/index.shtml");
  // 最大化
  driver.manage().window().maximize();
  // tx登录按钮在一个子iframe里,所以要把driver转向这个iframe
  WebDriver sd = driver.switchTo().frame(driver.findElement(By.id("loginIframe")));
  • 切换到密码登录
// 打开主页面的时候,是弹出二维码登录,所以需要找到切换按钮,切换成账号密码登录
WebElement sw = sd.findElement(By.id("switcher_plogin"));
// 登录按钮点击
sw.click();
  • 输入账号密码
driver.findElement(By.id("u")).sendKeys("your qq number");
driver.findElement(By.id("p")).sendKeys("your password");
  • 点击登录
driver.findElement(By.id("login_button")).click();
  • 获取cookie
// 将cookie全部打出到控制台
driver.manage().getCookies().stream().forEach(c -> System.out.println(c.toString()));

获取登录数据

在登录后的一堆数据中,我们需要拿到p_skey这个参数,但是p_skey这个cookie的domain是game.qq,而当前的url为lol.qq,直接输出cookie是拿不到这个值的,需要先重定向到game.qq

  driver.get("https://game.qq");

不能通过执行js打开另外一个window,因为如果这样原window的cookie就拿不到了。

然后在cookie中就会发现p_skey这个参数

后续的操作只需要拿着这个p_skey即可访问各种接口

完整代码

		System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe");

        ChromeDriverProxy driver = null;

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--disable-popup-blocking"); // 禁用阻止弹出窗口
        options.addArguments("no-sandbox"); // 启动无沙盒模式运行
        options.addArguments("disable-extensions"); // 禁用扩展
        options.addArguments("no-default-browser-check"); // 默认浏览器检查
        Map<String, Object> prefs = new HashMap();
        prefs.put("credentials_enable_service", false);
        prefs.put("profile.password_manager_enabled", false);// 禁用保存密码提示框
        options.setExperimentalOption("prefs", prefs);

        // set performance logger
        // this sends Network.enable to chromedriver
        LoggingPreferences logPrefs = new LoggingPreferences();
        logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
        logPrefs.enable(LogType.BROWSER, Level.ALL);
        options.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
        options.setExperimentalOption("w3c", false);
        // if your wantn't open the chrome head, you can set it true.
		// options.setHeadless(true);

        driver = new ChromeDriverProxy(options);
        driver.get("https://lol.qq/space/index.shtml");
		// login.
        driver.manage().window().maximize();
        WebDriver sd = driver.switchTo().frame(driver.findElement(By.id("loginIframe")));
        WebElement sw = sd.findElement(By.id("switcher_plogin"));
        sw.click();
        driver.findElement(By.id("u")).sendKeys("your qq number");
        driver.findElement(By.id("p")).sendKeys("your qq password");
        driver.findElement(By.id("login_button")).click();
        System.out.println(driver.getCurrentUrl());

		// select the lol area.
        Thread.sleep(2000);
        WebElement area = driver.findElement(By.id("areaContentId_lol"));
        Select select = new Select(area);
        select.selectByValue("19");
        driver.findElement(By.id("roleselecterlol")).click();
        Thread.sleep(2000);
        area.click();
        driver.findElement(By.id("confirmButtonId_lol")).click();
        Thread.sleep(1500);
        
        // get cookie.
        String newWindowUrl = "https://game.qq/";
        driver.get(newWindowUrl);
        driver.manage().getCookies().stream().forEach(c -> System.out.println(c.toString()));
        driver.manage().getCookies().stream().forEach(c -> System.out.println(c.getName() + "\t" + c.getValue()));
        pSkey = driver.manage().getCookieNamed("p_skey").getValue();
        System.out.println(pSkey);
        System.out.println(driver.manage().getCookieNamed("skey").getValue());

总结

通过selenium的方式登录无验证的QQ 方便快捷,而且稳定性比较高。所以本人是比较推荐使用这种方法去登录的。

最终的效果图如下:

下篇文章中将介绍有验证码的情况下的登录方式。

本文标签: seleniumqq