1 from selenium import webdriver 2 from selenium.webdriver.support.ui import WebDriverWait # 等待元素加载的 3 from selenium.webdriver.common.action_chains import ActionChains # 拖拽 4 from selenium.webdriver.support import expected_conditions as EC 5 from selenium.common.exceptions import TimeoutException, NoSuchElementException 6 from selenium.webdriver.common.by import By 7 from selenium.webdriver.common.keys import Keys 8 from PIL import Image 9 import requests 10 import time 11 import re 12 import random 13 from io import BytesIO 14 15 16 def merge_image(image_file, location_list): 17 """ 18 拼接图片 19 :param image_file: 20 :param location_list: 21 :return: 22 """ 23 im = Image.open(image_file) 24 im.save('code.jpg') 25 new_im = Image.new('RGB', (260, 116)) 26 # 把无序的图片 切成52张小图片 27 im_list_upper = [] 28 im_list_down = [] 29 # print(location_list) 30 for location in location_list: 31 # print(location['y']) 32 if location['y'] == -58: # 上半边 33 im_list_upper.append(im.crop((abs(location['x']), 58, abs(location['x']) + 10, 116))) 34 if location['y'] == 0: # 下半边 35 im_list_down.append(im.crop((abs(location['x']), 0, abs(location['x']) + 10, 58))) 36 37 x_offset = 0 38 for im in im_list_upper: 39 new_im.paste(im, (x_offset, 0)) # 把小图片放到 新的空白图片上 40 x_offset += im.size[0] 41 42 x_offset = 0 43 for im in im_list_down: 44 new_im.paste(im, (x_offset, 58)) 45 x_offset += im.size[0] 46 return new_im 47 48 49 def get_image(driver, div_path): 50 ''' 51 下载无序的图片 然后进行拼接 获得完整的图片 52 :param driver: 53 :param div_path: 54 :return: 55 ''' 56 time.sleep(2) 57 background_images = driver.find_elements_by_xpath(div_path) 58 location_list = [] 59 # image_url = '' 60 for background_image in background_images: 61 location = {} 62 result = re.findall('background-image: url\("(.*?)"\); background-position: (.*?)px (.*?)px;', 63 background_image.get_attribute('style')) 64 # print(result) 65 location['x'] = int(result[0][1]) 66 location['y'] = int(result[0][2]) 67 68 image_url = result[0][0] 69 location_list.append(location) 70 image_url = image_url.replace('webp', 'jpg') 71 # '替换url http://static.geetest.com/pictures/gt/579066de6/579066de6.webp' 72 image_result = requests.get(image_url).content 73 image_file = BytesIO(image_result) # 是一张无序的图片 74 image = merge_image(image_file, location_list) 75 76 return image 77 78 79 def get_track(distance): 80 ''' 81 拿到移动轨迹,模仿人的滑动行为,先匀加速后匀减速 82 匀变速运动基本公式: 83 ①v=v0+at 84 ②s=v0t+(1/2)at² 85 ③v²-v0²=2as 86 87 :param distance: 需要移动的距离 88 :return: 存放每0.2秒移动的距离 89 ''' 90 # 初速度 91 v = 0 92 # 单位时间为0.2s来统计轨迹,轨迹即0.2内的位移 93 t = 0.2 94 # 位移/轨迹列表,列表内的一个元素代表0.2s的位移 95 tracks = [] 96 # 当前的位移 97 current = 0 98 # 到达mid值开始减速 99 mid = distance * 7 / 8 100 101 distance += 10 # 先滑过一点,最后再反着滑动回来 102 # a = random.randint(1,3) 103 while current < distance: 104 if current < mid: 105 # 加速度越小,单位时间的位移越小,模拟的轨迹就越多越详细 106 a = random.randint(2, 4) # 加速运动 107 else: 108 a = -random.randint(3, 5) # 减速运动 109 110 # 初速度 111 v0 = v 112 # 0.2秒时间内的位移 113 s = v0 * t + 0.5 * a * (t ** 2) 114 # 当前的位置 115 current += s 116 # 添加到轨迹列表 117 tracks.append(round(s)) 118 119 # 速度已经达到v,该速度作为下次的初速度 120 v = v0 + a * t 121 122 # 反着滑动到大概准确位置 123 for i in range(4): 124 tracks.append(-random.randint(2, 3)) 125 for i in range(4): 126 tracks.append(-random.randint(1, 3)) 127 return tracks 128 129 130 def get_distance(image1, image2): 131 ''' 132 拿到滑动验证码需要移动的距离 133 :param image1:没有缺口的图片对象 134 :param image2:带缺口的图片对象 135 :return:需要移动的距离 136 ''' 137 # print('size', image1.size) 138 139 threshold = 50 140 for i in range(0, image1.size[0]): # 260 141 for j in range(0, image1.size[1]): # 160 142 pixel1 = image1.getpixel((i, j)) 143 pixel2 = image2.getpixel((i, j)) 144 res_R = abs(pixel1[0] - pixel2[0]) # 计算RGB差 145 res_G = abs(pixel1[1] - pixel2[1]) # 计算RGB差 146 res_B = abs(pixel1[2] - pixel2[2]) # 计算RGB差 147 if res_R > threshold and res_G > threshold and res_B > threshold: 148 return i # 需要移动的距离 149 150 151 def main_check_code(driver, element): 152 """ 153 拖动识别验证码 154 :param driver: 155 :param element: 156 :return: 157 """ 158 image1 = get_image(driver, '//div[@class="gt_cut_bg gt_show"]/div') 159 image2 = get_image(driver, '//div[@class="gt_cut_fullbg gt_show"]/div') 160 # 图片上 缺口的位置的x坐标 161 162 # 2 对比两张图片的所有RBG像素点,得到不一样像素点的x值,即要移动的距离 163 l = get_distance(image1, image2) 164 # 3 获得移动轨迹 165 track_list = get_track(l) 166 print('第一步,点击滑动按钮') 167 ActionChains(driver).click_and_hold(on_element=element).perform() # 点击鼠标左键,按住不放 168 print('第二步,拖动元素') 169 for track in track_list: 170 ActionChains(driver).move_by_offset(xoffset=track, yoffset=0).perform() # 鼠标移动到距离当前位置(x,y) 171 time.sleep(0.002) 172 173 ActionChains(driver).move_by_offset(xoffset=-random.randint(2, 5), yoffset=0).perform() 174 print('第三步,释放鼠标') 175 ActionChains(driver).release(on_element=element).perform() 176 time.sleep(1) 177 178 179 def main_check_slider(driver): 180 """ 181 检查滑动按钮是否加载 182 :param driver: 183 :return: 184 """ 185 while True: 186 try: 187 # driver.get('http://117.71.57.99:9080/online/') 188 # driver.maximize_window() 189 # login_button = WebDriverWait(driver, 10, 0.5).until( 190 # EC.presence_of_element_located((By.XPATH, "//h3[contains(text(),'登录')]"))) 191 # login_button.click() 192 # username = driver.find_element_by_id('accountCode') 193 # pwd = driver.find_element_by_id('accountPass') 194 # username.clear() 195 # username.send_keys('ahzy') 196 # time.sleep(1) 197 # pwd.clear() 198 # pwd.send_keys('ahzy3445810') 199 # pwd.send_keys(Keys.ENTER) 200 # time.sleep(2) 201 driver.get('http://117.71.57.99:9080/online/') 202 driver.implicitly_wait(1) 203 driver.maximize_window() 204 driver.get('http://117.71.57.99:9080/online/roomResource.xp?action=showResource') 205 # 2、点击按钮,得到没有缺口的图片 206 element = WebDriverWait(driver, 10, 0.5).until( 207 EC.presence_of_element_located((By.XPATH, '//div/div[@class="gt_slider_knob gt_show"]'))) 208 if element: 209 return element 210 except TimeoutException as e: 211 print('超时错误,继续') 212 time.sleep(5) 213 214 215 if __name__ == '__main__': 216 try: 217 count = 6 # 最多识别6次 218 driver = webdriver.Chrome() 219 # 等待滑动按钮加载完成 220 element = main_check_slider(driver) 221 while count > 0: 222 main_check_code(driver, element) 223 try: 224 success_element = (By.CSS_SELECTOR, '.gt_holder .gt_ajax_tip.gt_success') 225 # 得到成功标志 226 success_images = WebDriverWait(driver, 20).until(EC.presence_of_element_located(success_element)) 227 if success_images: 228 print('成功识别!!!!!!') 229 count = 0 230 break 231 except NoSuchElementException as e: 232 print('识别错误,继续') 233 count -= 1 234 else: 235 print('too many attempt check code ') 236 exit('退出程序') 237 finally: 238 driver.close()
转载于:https://www.cnblogs.com/wjlv/p/11605291.html