Java + selenium + cookie 绕过 reCAPTCHA 验证的登录页面

1. 如果网站有记住登录状态的功能,可以直接用 new cookie 来创建cookies, cookies 可以用F12 或者fiddler 来抓包查看

driver.get("https://xxxxxx/");
driver.manage().deleteAllCookies();
Cookie cookie1 = new Cookie("name1", "xxxxxx");
Cookie cookie2 = new Cookie("name2", "xxxx");
driver.manage().addCookie(cookie1);
driver.manage().addCookie(cookie2);
driver.navigate().refresh();

2. 如果网站没有记住登录状态,但是可以调用特定的API 去获取token, 用来更新cookies,然后add cookies 后就可以去登录后的页面了。

WebDriver driver = new ChromeDriver();
driver.get("https://xxxxx/login.html");
//get cookies for login
Map cookies = getCookie.getCookieValue();
Cookie cookie1 = new Cookie.Builder("amlbcookie",cookies.get("amlbcookie").toString())
        .domain(".xxx.com")
        .isHttpOnly(true)
        .isSecure(true)
        .path("/")
        .build();
Cookie cookie2 = new Cookie.Builder("ms-sso",cookies.get("ms-sso").toString())
        .domain(".xxx.com")
        .isHttpOnly(true)
        .isSecure(true)
        .path("/")
        .build();
driver.manage().addCookie(cookie1);
driver.manage().addCookie(cookie2);
Thread.sleep(2000);
System.out.println("***** Let's Go To The Home Page ! ******");
driver.get("https://xxx.com/#/homePage/home");

 

你可能感兴趣的:(test,skills,Selenium,JAVA)