Selenium+Java automation how to elegantly bypass the verification code

Selenium+Java automation how to elegantly bypass the verification code

Selenium+Java automation how to elegantly bypass the verification code

Walking Heart Original Issue No. 15
Selenium+Java automation how to elegantly bypass the verification code

Preface

The verification code problem is a painful problem for every student of UI automation. I personally do not advocate cracking the verification code. Don't think about cracking methods. This verification code was originally designed to prevent others from automatically logging in. If your company’s verification code is easily cracked by you, then it can only be said that your company’s verification code level is not high, and it is recommended to develop and improve the verification code level.
For the verification code, either let the developer write the verification code (the legendary universal verification code), such as: 1234, or try to bypass it in a low-key manner.
The following article introduces how to bypass the verification code in the figure below and enter the cookie operation of the blog garden
Selenium+Java automation how to elegantly bypass the verification code
tool
Fiddler.exe
IDEA/Eclipse
selenium 1.
Use Fiddler to capture packets
1. Generally, after successfully logging in to the website, a logged-in cookie will be generated. Then just get this value directly and use selenium for addCookie operation.
2. You can manually log in once, and then grab this cookie, here we need to use the capture tool fiddler
3. First open the blog garden login interface, manually enter the account and password (don’t click the login button)
Selenium+Java automation how to elegantly bypass the verification code
4. Open fiddler to capture the packet Tool, click the blog garden login button at this time.
Selenium+Java automation how to elegantly bypass the verification code
5. After logging in successfully, check the cookie changes and find that there are two more sets of parameters. The more two sets of parameters are what we want. Copy it out, it will be useful for one.
Selenium+Java automation how to elegantly bypass the verification code
Second, cookie operation
1. Two cookies need to be added here, one is .CNBlogsCookie and the other is .Cnblogs.AspNetCore.Cookies.
2. The webpage I opened here is the homepage of Blog Garden: https://home.cnblogs.com/, I didn't enter the login page .
3. Refresh after adding the cookie, and then it's time to witness the miracle.
4. Regarding cookie operation, interested students can check the official api
5. Here we only use the method of the addCookie interface to implement
Cookie cookie=new Cookie("key", "value"); driver.manage().addCookie(cookie );
Three, reference code

package com.test.demo;

import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;

import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author longrong.lang
 * 绕过验证码登陆
 */
public class LoginWithCookie {

    WebDriver driver;

    @BeforeClass
    public void beforeClass() {
        driver = new ChromeDriver();
    }

    @Test
    public void testLoginWithCookie() {
        driver.get("https://home.cnblogs.com/");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        //cookie遍历使用
        Set<Cookie> cookies = driver.manage().getCookies();
        for (Cookie cookie : cookies) {
            System.out.println(cookie.getName() + "\t" + cookie.getValue());
        }
        Cookie cookie = new Cookie("key", "value");
        //添加cookie操作
        driver.manage().addCookie(cookie);
        Cookie cookie1 = new Cookie("key", "value");
        driver.manage().addCookie(cookie1);
        Cookie cookie2 = new Cookie(".key", "value");
        driver.manage().addCookie(cookie2);
        Cookie cookie3 = new Cookie("key", "value");
        driver.manage().addCookie(cookie3);
        driver.manage().window().maximize();
        //下面是见证奇迹的时候了
        driver.navigate().refresh();
    }

    @AfterClass
    public void afterClass() {
        //driver.quit();
    }
}

Effect picture: The
Selenium+Java automation how to elegantly bypass the verification code
video effect is as follows:

There are a few points to note:
1. Check the next automatic login button when logging in.
2.addCookie() only adds name and value, which is unsuccessful for logging in to the blog garden.
3. This method is not suitable for all websites, it is generally suitable for those who remember the login status like blog garden.
4. If you have any questions during the learning process, you can add QQ group to communicate: 631646987
Selenium+Java automation how to elegantly bypass the verification code

  • End -
    Author: Brain hole big food goods Leo likes specializes in technology, diligent summarized, make friends with words, good and love to kill one life, happy to share a technical madman.
    Software Tester
    Selenium+Java automation how to elegantly bypass the verification code

I heard that this account can kill the leader

· Guess the article you like·

Guess you like

Origin blog.51cto.com/15009374/2555983