In the course of web automation testing, you often get stuck with the login CAPTCHA and don't know how to go about passing the CAPTCHA.
In general when encountering a CAPTCHA we can all can get the development to help solve the problem, turn off the CAPTCHA, or give a universal CAPTCHA!
So is there any way we can handle these CAPTCHA issues ourselves if the devs don't help?
The answer, of course, is yes. Common CAPTCHAs are generally categorized into two types, a graphic CAPTCHA and a sliding CAPTCHA!
Slide Verification Hacking Ideas
The idea about sliding CAPTCHA hack is broadly speaking the following two steps:
- 1, get the slider sliding distance
- 2. Simulate dragging the slider and pass the validation.
Regarding this sliding CAPTCHA, both the slider and the notched background are each a separate image, and we can take these two images, the
Download it with the help of image recognition to recognize the position of the notch in the background image, and then subtract the current position of the slider to arrive at the distance that needs to be slid.
case study
Without further ado, let's take a look at a case (QQ space login), QQ space login case implementation steps are as follows:
- 1, create a driver object, access qq login page
- 2、Input the account password
- 3、Click to login
- 4. Analog sliding validation
Implementation Code
import time from selenium import webdriver from slideVerfication import SlideVerificationCode # 1, create a driver object, access qq login page browser = () ("/") # 2, enter the account password # 2.0 Click to switch to login iframe browser.switch_to.frame('login_frame') # 2.1 Click Account Password to Login browser.find_element_by_id('switcher_plogin').click() # 2.2 Locate the account number entry box and enter the account number browser.find_element_by_id("u").send_keys("123456") # 2.3 Locate Password Entry Enter the password browser.find_element_by_id("p").send_keys("PYTHON") # 3. Click Login browser.find_element_by_id('login_button').click() (3) # 4. Simulated Slide Verification # 4.1 Switching to the sliding captcha iframe tcaptcha = browser.find_element_by_id("tcaptcha_iframe") browser.switch_to.frame(tcaptcha) # 4.2 Getting slide-related elements # Select the node on which to drag the slider slide_element = browser.find_element_by_id('tcaptcha_drag_thumb') # Get the node of the slider image slideBlock_ele = browser.find_element_by_id('slideBlock') # Get notch background image node slideBg = browser.find_element_by_id('slideBg') # 4.3 Calculate sliding distance sc = SlideVerificationCode(save_image=True) distance = sc.get_element_slide_distance(slideBlock_ele,slideBg) # Sliding distance error correction, sliding distance * zoom ratio of the image displayed on the web page - initial position of the slider relative to the distance = distance*(280/680) - 22 print("Corrected sliding distance",distance) # 4.4. Make a slide sc.slide_verification(browser,slide_element,distance=100)
Running effects:
In fact, about this module image recognition, is with the help of a third-party image processing module to recognize, python has a lot of ready-made libraries used to deal with images, this article uses opencv-python to recognize. slideVerfication module above the two methods used in the part of the reference code is as follows:
Based on the incoming slider, and the nodes in the background, calculate the distance of the slider
def get_element_slide_distance(self, slider_ele, background_ele, correct=0): """ Based on the nodes of the incoming slider, and the background, the distance of the slider is calculated This method only calculates the distance between the slider and the background image if the slider and the background image are both one complete image. If the background image is a background image stitched together by multiple small images, the This method is not applicable, please use get_image_slide_distance this method :param slider_ele: node of the slider image :type slider_ele: WebElement :param background_ele: Node for the background image :type background_ele:WebElement :param correct: slider notch screenshot correction value, default is 0, debugging the screenshot is the case of the correct value will be used :type: int :return: the x coordinate of the notch position of the background image (the left border of the notch image) """ # Get the image of the CAPTCHA slider_url = slider_ele.get_attribute("src") background_url = background_ele.get_attribute("src") # Download Captcha background image, slide image slider = "" background = "" self.onload_save_img(slider_url, slider) self.onload_save_img(background_url, background) # Read the colorimetric image, convert it to array type data in numpy. slider_pic = (slider, 0) background_pic = (background, 0) # Get the shape of the notch map array --> width and height of the notch map width, height = slider_pic.shape[::-1] # Save the processed image as slider01 = "" background_01 = "" (background_01, background_pic) (slider01, slider_pic) # Read a saved slider map slider_pic = (slider01) # Perform color conversions slider_pic = (slider_pic, cv2.COLOR_BGR2GRAY) # Get the absolute value of the color difference slider_pic = abs(255 - slider_pic) # Save the picture (slider01, slider_pic) # Read the slider slider_pic = (slider01) # Read the background image background_pic = (background_01) # Compare the area of overlap between the two maps result = (slider_pic, background_pic, cv2.TM_CCOEFF_NORMED) # Get the notch position of the image top, left = np.unravel_index((), ) # Image notch coordinates position in background image print("Current slider notch position:", (left, top, left + width, top + height)) return left
Slide the slider to verify
def slide_verification(self, driver, slide_element, distance): """ Slide the slider to verify :param driver: driver object :type driver. :param slide_element: Tuple of sliders :type slider_ele: WebElement :param distance: distance of the slide :type: int :return. """ # Get the url of the page before the slide start_url = driver.current_url print("The distance to be slid is:", distance) # Generate sliding trajectories based on sliding distances locus = self.get_slide_locus(distance) print("The generated sliding trajectory is:{},The sum of the distances of the trajectories is{}".format(locus, distance)) # Press the left mouse button ActionChains(driver).click_and_hold(slide_element).perform() (0.5) # Traverse the trajectory to slide for loc in locus: (0.01) ActionChains(driver).move_by_offset(loc, (-5, 5)).perform() ActionChains(driver).context_click(slide_element) # Release the mouse ActionChains(driver).release(on_element=slide_element).perform()
This is the end of this article about python implementation of hacking sliding captcha. I hope it will be helpful to your learning, and I hope you will support me more.