When python beginners challenge JD's slide verification code at the beginning, it's a bit difficult. I've also studied it for a long time. It's not only the problem of pictures but also the problem of codes. There are also various problems that beginners encounter (all of which can make people tangle for half a day), such as less parentheses, less typing:, no indentation, etc. In the end, it's still a little bit of a change based on the code of the God to pass with low probability
Needless to say, there are two types of codes. The first type uses the complete original map, and the second type uses the notch original map (gray and binary processing). The final result is: the first type of code can't pass all the time because of the track problem, of course, the distance location is quite different from the actual situation; the second type of original code can't pass because of the track problem, I slightly added two Line code, the last low probability pass (if you are lucky, you can pass 2-3 times) (more than 10 times, it is recommended to exit and try again) [I think it may be the reason for the big error between the distance position and the actual situation], go to the code directly below.
First type code:
Reprint and reference link are, if there is any leakage, please inform:
https: // blog.csdn.net / qq_42709514 / article / details / 81975918
from PIL import Image from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains import time,random #Supplementary judgment module from selenium.webdriver.support import expected_conditions as EC #Judgement element from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait def get_image(browser): time.sleep(2) bigimg_element = browser.find_element_by_xpath('//Div / div [@ class = "jdjrv bigimg"] ') (no height may require a little reaction time, time.sleep(2) time.sleep(2) bigsize = bigimg_element.size #invalid syns will appear if there is less than one bracket in the previous line biglocation = bigimg_element.location bigleft = biglocation['x'] bigtop = biglocation['y'] bigright = bigleft + bigsize['width'] bigbottom = bigtop + bigsize['height'] print(bigsize) #In this paper, the purpose of multiple print is to verify whether the program can run normally and get some parameter values for judgment (such as gap distance print(loc)) print(bigsize['width']) print(bigsize['height']) time.sleep(1) print(bigleft, bigtop, bigright, bigbottom) browser.save_screenshot('D:\quekou.png') snap_obj = Image.open('D:\quekou.png') print(snap_obj.size) #For verification time.sleep(2) img_obj = snap_obj.crop((bigleft, bigtop, bigright, bigbottom)) #For what? # img_obj.save('D:\orimg.png') # img_obj.show() image = img_obj #If the location is image = get image (browser), it will cause a loop time.sleep(2) ad = match_source(image) time.sleep(2) visualstack = get_diff_location(ad, image) loc = visualstack print(loc) time.sleep(2) track_list = get_track7(loc) time.sleep(2) slide(browser,track_list) return img_obj def match_source(image): imagea = Image.open('C:/Users/Administrator/Desktop/jingdongimg/1 (1).png') imageb = Image.open('C:/Users/Administrator/Desktop/jingdongimg/1 (2).png') imagec = Image.open('C:/Users/Administrator/Desktop/jingdongimg/1 (3).png') imaged = Image.open('C:/Users/Administrator/Desktop/jingdongimg/1 (4).png') imagee = Image.open('C:/Users/Administrator/Desktop/jingdongimg/1 (5).png') imagef = Image.open('C:/Users/Administrator/Desktop/jingdongimg/1 (6).png') imageg = Image.open('C:/Users/Administrator/Desktop/jingdongimg/1 (7).png') imageh = Image.open('C:/Users/Administrator/Desktop/jingdongimg/1 (8).png') imagei = Image.open('C:/Users/Administrator/Desktop/jingdongimg/1 (9).png') imagej = Image.open('C:/Users/Administrator/Desktop/jingdongimg/1 (10).png') list = [imagea, imageb, imagec, imaged,imagee,imagef,imageg,imageh,imagei,imagej] # list = [imagea, imageb, imagec] #Search pixel difference, traverse the original image for i in list: # The original drawing of my computer is the same as that of the slide corresponding to the notch drawing. The original drawing of the vertical coordinate is 88px larger than that of the notch drawing, which can be modified according to the actual situation pixel1 = image.getpixel((200,100 )) pixel2 = i.getpixel((200, 100)) # pixel represents R value, pixel represents G value, pixel represents B value if abs(pixel1 - pixel2) < 5: return i return image # Get gap position 1 (get gap position 1: use the method of setting threshold; get gap position 2 use the method of comparing RGB, but for JD, the difference between the two methods is quite large, which may also be the reason why I set it) def get_diff_location(image1,image2): left = 43 for i in range(left, image1.size): for j in range(image1.size): if not is_pixel_equal(image1, image2, i, j): left = i return left return left def is_pixel_equal(image1,image2, x, y): """ //Determine whether two pixels are the same :param image1: Picture 1 :param image2: Picture 2 :param x: position x :param y: position y :return: Whether the pixels are the same """ # Take the pixels of two pictures pix1 = image1.load()[x, y] pix2 = image2.load()[x, y] threshold = 60 if (abs(pix1 - pix2 < threshold) and abs(pix1 - pix2 < threshold) and abs( pix1 - pix2 < threshold)): return True else: return False # Get gap position 2 # def get_diff_location(image1,image2): # (40270) (10100), the slider size is 38 * 38, the length of the image search area is (40270), and the height is (10100), which can be modified according to the actual situation # for i in range(40,270): # for j in range(10,100): # #Traversing the original image and notch image prime value to find the notch location # if is_similar(image1,image2,i,j)==False: # return i # return -1 # # Compare RGB value to gap position # def is_similar(image1,image2,x,y): # # pixel1=image1.getpixel((x, y)) # pixel1 = image1.getpixel((x, y)) # pixel2=image2.getpixel((x, y)) # # There may be errors in the screenshot pixels, 50 as the tolerance range (refer to Titan's, no change) # if abs(pixel1-pixel2)>=50 and abs(pixel1-pixel2)>=50 and abs(pixel1-pixel2)>=50: # return False # return True def get_track7(distance):(Refer to the God's, no change) # """ # Calculate the moving track according to the offset and manual operation simulation # : param distance: offset # : return: move track # """ # Moving trajectory tracks= # Current displacement current = 0 # Deceleration threshold mid = distance * 4 / 5 # time interval t = 0.2 # Initial velocity v = 0 while current < distance: if current < mid: a = random.uniform(7, 9) else: a = -(random.uniform(12.5, 13.5)) v0 = v+1 v = v0 + a * t x = v0 * t + 1 / 2 * a * t * t current += x if 0.6 < current - distance < 1: x = x - 0.53 tracks.append(round(x, 2)) elif 1 < current - distance < 1.5: x = x - 1.4 tracks.append(round(x, 2)) elif 1.5 < current - distance < 3: x = x - 1.8 tracks.append(round(x, 2)) else: tracks.append(round(x, 2)) print(tracks, sum(tracks)) return tracks def slide(browser,track_list): slideblock=browser.find_element_by_class_name('JDJRV-slide-inner.JDJRV-slide-btn') ActionChains(browser).click_and_hold(slideblock).perform() for track in track_list: ActionChains(browser).move_by_offset(xoffset=track,yoffset=0).perform() ActionChains(browser).move_by_offset(xoffset=1, yoffset=0).perform() ActionChains(browser).pause(random.randint(6,14)/10).release(slideblock).perform() #Try after failure try: failure = WebDriverWait(browser, 20,5).until(EC.text_to_be_present_in_element((By.CLASS_NAME, 'JDJRV-slide-center'), 'Slide right to complete the puzzle')) #(with reference to the great God, this part is changed to Jingdong) #Failure = webdriverwait (browser, 5). Until (EC. Text \ print(failure) except: print('Verify success') return None if failure: get_image(browser) # self.crack_slider(browser) browser = webdriver.Firefox() browser.get('https://www.jd.com/') browser.find_element_by_link_text("Hello, please log in").click() elem=browser.find_element_by_class_name("login-tab.login-tab-r").click() #u #This will work, but not the next line. username='******' #Input account passwd='******' #Input password elem=browser.find_element_by_id("loginname") #u elem.send_keys(username) elem=browser.find_element_by_id("nloginpwd") #p elem.send_keys(passwd) elem=browser.find_element_by_id("loginsubmit")#.submit() #Different web pages find different by id or class name elem.click() # Calling function get_image(browser) # # -------- //Reprint and reference contents are as follows. Please inform us if there is any omission: # # Original link: https://blog.csdn.net/qq_39802740/article/details/83584980 # Original link: https://blog.csdn.net/qq_/article/details/81187105 # # # Original link: https://blog.csdn.net/qq_42709514/article/details/81975918
Category 2 Code:
Reprint and reference link are, thank God:
import cv2 import time import numpy as np from selenium import webdriver from urllib import request from selenium.webdriver.common.action_chains import ActionChains #Supplementary judgment module (the following modules are added on the basis of the God) from selenium.webdriver.support import expected_conditions as EC #Judgement element from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait def loadpage(browser,username, password): browser.get('https://passport.jd.com/new/login.aspx? ', open the JD website browser.find_element_by_class_name("login-tab.login-tab-r").click() # Click account password to log in browser.find_element_by_id("loginname").send_keys(username) # u browser.find_element_by_id("nloginpwd").send_keys(password) # p browser.find_element_by_id("loginsubmit").click() # Click login time.sleep(2) getPic(browser) while True:(On the basis of the big God code, we have modified some) try: failure = WebDriverWait(browser, 10, 2).until(EC.text_to_be_present_in_element((By.CLASS_NAME, 'JDJRV-slide-center'), 'Slide right to complete the puzzle')) print('failure') except: print('Verify success') return None if failure: getPic(browser) def getPic(browser):(Using the code of the God, basically unchanged) # Large image for finding login image s2 = r'//div/div[@class="JDJRV-bigimg"]/img' # Small slider to find login image s3 = r'//div/div[@class="JDJRV-smallimg"]/img' bigimg = browser.find_element_by_xpath(s2).get_attribute("src") smallimg = browser.find_element_by_xpath(s3).get_attribute("src") # print(smallimg + '\n') # print(bigimg) # Naming of large background map backimg = "backimg.png" # Slider naming slideimg = "slideimg.png" # Download background map and save it locally request.urlretrieve(bigimg, backimg) # Download slider to save locally request.urlretrieve(smallimg, slideimg) # Take pictures and grayscale them block = cv2.imread(slideimg, 0) template = cv2.imread(backimg, 0) # Binary image name #High recognition rate of grayed image blockName = "block.jpg" templateName = "template.jpg" # Save the binary image cv2.imwrite(blockName, block) cv2.imwrite(templateName, template) block = cv2.imread(blockName) block = cv2.cvtColor(block, cv2.COLOR_RGB2GRAY) block = abs(255 - block) cv2.imwrite(blockName, block) block = cv2.imread(blockName) template = cv2.imread(templateName) # Get offset result = cv2.matchTemplate(block, template, cv2.TM_CCOEFF_NORMED) # Find the location of the block in the template and return the result as a matrix, which is the matching result of each point x, y = np.unravel_index(result.argmax(), result.shape) print('Previous code can operate normally') # Get the slider (based on the God, change the point to simulate the human effect) element = browser.find_element_by_xpath(s3) ActionChains(browser).click_and_hold(on_element=element).perform() #time.sleep() #You can't add time.sleep to this line, because after a pause, JD basically thinks you're a robot ActionChains(browser).move_to_element_with_offset(to_element=element, xoffset=y*4/5, yoffset=0).perform() #I changed y to y*4/5 on the basis of God time.sleep(0.2) #First line added by myself ActionChains(browser).move_to_element_with_offset(to_element=element, xoffset=y -y*7/10, yoffset=0).perform() #I added the second line to simulate human behavior ActionChains(browser).release(on_element=element).perform() time.sleep(3) #Open the web page and enter the account password browser = webdriver.Firefox() loadpage(browser,"***"',"***") #The second is to enter JD account number and the third is to enter JD password # # *Reprint and reference https://www.52pojie.cn/thread-877390-1-1.html
Jingdong verification code is very difficult for my little white. There are many things I don't understand about the above two types of codes, mainly referring to the code of God. To be honest, the passing rate of JD verification code is a little low (one is that the location is not accurate, the other is that it is doubted whether it is artificial operation), but I was satisfied to pass by chance at last. After several days of research, JD verification code is over. Thank you for your code.