There are two ways to crack the slider verification code:
- Obtain a complete background image and a gap image. Compare the two images one by one on the pixels to find out the different coordinates.
- Get a picture with a gap and a small picture that needs to be verified. Two pictures are bipolarized and normalized to determine the coordinates of the small picture in the middle of the picture.
- After that, we will use junior high school physics knowledge, use linear acceleration to imitate human manual operation
The second one is used this time. The first one is relatively simple. Don't talk too much, just go to the code:
- The following are obtained by headless browser
- Get a small picture of slider verification
def get_image1(self,driver): """ //Get small picture of verification gap of slider :param driver:chrome object :return:Small picture of gap """ canvas = driver.find_element_by_xpath("//div[@id='xy_img']").get_attribute("style") image_data=re.findall("data:image/jpg;base64,(.*?)\"\)",canvas)[0] # print(image_data) binary_image_data=base64.b64decode(image_data,'-_') file_like=BytesIO(binary_image_data) image=Image.open(file_like) return image
Generally speaking, this small picture is independent and easy to get. The picture is as follows:
- Get a background picture for slider verification
!!! This background image web page will generally return the disordered images, and then reorder the images through js. It takes a lot of time to crack, and each js sorting algorithm is not the same, and does not have reusability. Here is a trick. Take a screenshot of the current browser directly, and then take a picture of the specified range.
def get_image2(self,driver): """ //Get slider verification code background picture :param driver:chrome object :return:Background picture """ driver.save_screenshot('yanzhengma.png') # Get coordinate value through picture element node # element = driver.find_element_by_id("bgImg") # left = element.location['x'] # top = element.location['y'] # right = element.location['x'] + element.size['width'] # bottom = element.location['y'] + element.size['height'] # Get the coordinate value of the corresponding picture directly through the drawing software left=359 top=238 right=658 bottom=437 # print((left, top, right, bottom)) im = Image.open('yanzhengma.png') im = im.crop((left, top, right, bottom)) return im
The picture is as follows:
- Track calculation method
def get_track(self, distance): """ //Get the moving track according to the offset :param distance:Offset :return:Moving trajectory """ # Moving trajectory track = [] # Current displacement current = 0 # Deceleration threshold mid = distance * 4 / 5 # Computation interval t = 0.2 # Initial velocity v = 0 while current < distance: if current < mid: # Acceleration is positive 2 a = 2 else: # Acceleration is negative 3 a = -3 # Initial speed v0 v0 = v # Current speed v = v0 + at v = v0 + a * t # Moving distance x = v0t + 1/2 * a * t^2 move = v0 * t + 1 / 2 * a * t * t # Current displacement current += move # Entry trajectory track.append(round(move)) return track
- Verify main program
def slider_verification_code(self,driver,cnt): """ //Break the slider to verify the main program :param driver:chrome Object; cnt: Verified times :return:Verified times """ print("Slider verification appears, in verification") # 1. The slider verification appears to get the verification picture picture1 = self.get_image1(driver) picture1.save("./picture1.png") # 2. Obtain the gap verification picture picture2 = self.get_image2(driver) picture2.save("./picture2.png") #Binary image, compare, output matching coordinate system target_rgb=cv2.imread("./picture2.png") target_gray=cv2.cvtColor(target_rgb,cv2.COLOR_BGR2GRAY) template_rgb=cv2.imread("./picture1.png",0) res=cv2.matchTemplate(target_gray,template_rgb,cv2.TM_CCOEFF_NORMED) value=cv2.minMaxLoc(res) value = value[3][0] cnt += 1 print("The distance to be displaced is:"+str(value)+",Verified"+str(cnt)+"second") #Obtain the track route of displacement according to the distance track=self.get_track(value) time.sleep(1) ActionChains(driver).click_and_hold(driver.find_element_by_class_name("handler.handler_bg")).perform() for x in track: ActionChains(driver).move_by_offset(xoffset=x, yoffset=0).perform() time.sleep(0.5) ActionChains(driver).release().perform() return cnt
Look! With aircraft: