The author is a beginner of Python crawler, through the book (jing Mi da Shen out of the book) to learn the way to break through the verification of simulated login has this type of application knowledge. This method does not involve any business relationship. If there is any violation, please contact the author

The implementation steps are divided into 3 steps:

  • 1. Enable the application to have validated full images and images with gaps
  • 2. Identify the location of the breach
  • 3. Drag the slider to the hole to complete the verification

Introduce relevant libraries

from selenium import webdriver;
from selenium.webdriver.support.wait import WebDriverWait;
from selenium.webdriver.support import expected_conditions as EC;
from selenium.webdriver.common.by import By;
from selenium.webdriver import ActionChains;
import time;
from PIL import Image;
from io import BytesIO;
Copy the code

We define a class to implement the related operations and define some configuration

EMAIL='xxx'  # account
PASSWORD='xxx'  # password (just a simple process)
BORDER=6; # The distance between the slipper and the left edge
INIT_LEFT=60; Start to detect the position of the notch from the X-axis direction, that is, X =60
Copy the code
class GrackGeetest(object):
    
    def __init__(self):
        # Here we start to define some related parameters (for example, we log in to the official website of Polar Check, other methods are similar)
        self.url='https://auth.geetest.com/login/';
        self.browser=webdriver.Chrome();
        self.wait=WebDriverWait(self.browser,20);
        self.email=EMAIL;
        self.password=PASSWORD;
        

# Implement step 1:
    def getGeetestButton(self): 
        Click on the button node element that causes the live validation diagram to appear and return
        button=self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'geetest_radar_tip')));
        return button;
    
    Get the position of the validation diagram in the web page and return it as a tuple
    def getImagePosition(self):
        geetestImage=self.wait.until(EC.presence_of_element_located((By.CLASS_NAME,'geetest_canvas_img')));
        time.sleep(2);
        location=geetestImage.location;
        size=geetestImage.size;
        top,bottom,left,right=location['y'],location['y']+size['height'],location['x'],location['x']+size['width'];
        return (top,bottom,left,right);
    
    # capture the current page
    def getChromePage(self):
        pageShot=self.browser.get_screenshot_as_png();
        pageShot=Image.open(BytesIO(pageShot));
        return pageShot;
    
    # Capture the verification image from the web page and return it
    def getGeetestImage(self,name='geetest.png'):
        top,bottom,left,right=self.getImagePosition();
        # Capture an image of the current page
        pageShot=self.getChromePage();
        # Capture the position of the validation diagram
        captchaImage=pageShot.crop((left,top,right,bottom));
        captchaImage.save(name);Save to the current folder
        return captchaImage;
    
Step 2: Identify the location of the gap
    def getSlider(self):
        Get a draggable slider object
        slider=self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'geetest_slider_button')));
        return slider;
    
    The gap position can be obtained by comparing the difference of pixels in the two images
    def getGap(self,image1,image2):
        left=60; 
        #size[0]->width,size[1]->height
        for i in range(left,image1.size[0]):
            for j in range(image1.size[1]):
                if not self.isPixelEqual(image1,image2,i,j):
                    # Since the small slider and the notch are on the same horizontal line, we only take the value in the X-axis
                    left=i;
                    return left;
        return left;
        
    
    def isPixelEqual(self,image1,image2,x,y):
        # check whether 2 pixels are the same
        pixel1=image1.load()[x,y]; #pixel1,pixel2 is an RGB value and is a tuple
        pixel2=image2.load()[x,y];
        When this threshold is exceeded, the two pixels do not match and are the pixels in the upper left corner of the notch
        threshold=60;
        if abs(pixel1[0]-pixel2[0])<threshold and abs(pixel1[1]-pixel2[1])<threshold and abs(pixel1[2]-pixel2[2])<threshold :
            return True;
        else:
            return False;
    
    # Step 3 Correlation method: The most critical step is also the step beyond the polar verification machine learning algorithm
    # Adopt the method of changing the acceleration of objects in physics by stages. Here, the method of accelerating first and then decelerating is adopted
    X =v0*t+1/2*a*t*t v=v0+a*t
        
    def getTrack(self,distance):
        # short offset
        # Movement trajectory
        tranck=[];
        # Current displacement
        current=0;
        # Threshold for starting deceleration
        mid=distance*4/5;
        # compute intervalT = 0.2;# velocity
        v=0;
        while current<distance:
            if current<mid:
                a=2;
            else:
                # Start slowing down
                a=-3;
            # velocity
            v0=v;
            # Current speed
            v=v0+a*t;
            # displacement
            move=v0*t+1/2*a*t*t;
            # Current displacement
            current+=move;
            # Add trajectory
            track.append(round(move));
        return track; 
            
    Move the slider according to the movement trajectory
    def moveToGap(self,slider,tracks):
        # Drag the slider to the gap
        ActionChains(self.browser).click_and_hold(slider).perform();
        for x intracks: ActionChains(self.browser).move_by_offset(xoffset=x,yoffset=0).perform(); Time. Sleep (0.5); ActionChains(self.browser).release().perform();# Finally simulate the login application
    def login(self):
        button=self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#base > div.content-outter > div > div > div:nth-child(3) > div > form > div:nth-child(5) > div > button')));
        button.click();
        time.sleep(10);
    
    
    The next direct implementation connects the entire process through a method
    def sendUserAndPassword(self):
        self.browser.get(self.url);
        Using the class selector, I copied the element directly from the browser, so it's longer. You can get it in other ways.
        email=self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#base > div.content-outter > div > div > div:nth-child(3) > div > form > div:nth-child(1) > div > div.ivu-input-wrapper.ivu-input-type.ivu-input-group.ivu-input-group-with-prepend > input')));
        password=self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#base > div.content-outter > div > div > div:nth-child(3) > div > form > div:nth-child(2) > div > div.ivu-input-wrapper.ivu-input-type.ivu-input-group.ivu-input-group-with-prepend > input')));
        email.send_keys(self.email);
        password.send_keys(self.password);
    
    
    
    
    def doVerifyLogin(self):
    Step # 1:
    
        Enter the account password
        self.sendUserAndPassword();
        # Click the verify button
        verifyButton=self.getGeetestButton();
        verifyButton.click();
        # Start getting 2 validation images
        image1=self.getGeetestImage('geetest1.png');
        Click the small slider to get the notched verification image
        slider=self.getSlider();
        slider.click();
        # Get the notched validation diagram
        image2=self.getGeetestImage('geetest2.png');
    
    Step # 2:
        Get the notch position
        gap=self.getGap(image1,image2);
        The position of the notch needs to be subtracted from the small slider by a small distance to the left
        gap-=BORDER;
    
    Step # 3:
        # Movement trajectory
        track=self.getTrack();
        # Drag slider
        self.moveToGap(slider,track);
        # Finally determine if it was successful. If not, redo the process
        try:
            success = self.wait.until(
                EC.text_to_be_present_in_element((By.CLASS_NAME, 'geetest_success_radar_tip_content'), 'Verification successful'))
            print(success)
            Try again after failure
            if not success:
                self.doVerifyLogin()
            else:
                self.login()
        except:
            self.doVerifyLogin();
    
    if __name__ == '__main__':
        crack = GrackGeetest();
        crack.doVerifyLogin();
Copy the code

The above is the complete code, at the same time need to install ChromeDriver, install the specific process to find a search engine to ask