from selenium import webdriver from selenium.webdriver import ChromeOptions from selenium.webdriver import ActionChains from selenium.webdriver.common.keys import Keys import random from PIL import Image import time option = ChromeOptions() option.add_argument("disable-infobars") #截取图片 def cup_image(driver): #保存浏览器全屏图片 driver.save_screenshot("xxx.png") image = driver.find_element_by_class_name( "geetest_canvas_img" ) left = image.location["x"] top = image.location["y"] right = left+image.size["width"] buttom = top + image.size["height"] print(left,top,right,buttom) #获取屏幕图片对象 image_obj = Image.open("xxx.png") #开始截取图片 image1 = image_obj.crop((left,top,right,buttom)) return image1 #获取没有缺口的图片 #获得没有缺口的照片 def get_image1(driver): js = ''' var x = document.getElementsByClassName("geetest_canvas_fullbg")[0].style.display="block"; ''' driver.execute_script(js) time.sleep(1) image1 = cup_image(driver) time.sleep(1) return image1 #对获取有缺口一张图片 def get_image2(driver): js = ''' var x = document.getElementsByClassName("geetest_canvas_fullbg")[0].style.display="none"; ''' driver.execute_script(js) time.sleep(1) image2 = cup_image(driver) time.sleep(1) return image2 #对比两张图片获得滑动距离 def get_distance(img1,img2): #滑动初始值 start = 60 #像素差值 color_num= 60 #循环遍历图片的宽与高取每一个位置的x与y轴 for x in range(start,img1.size[0]): for y in range(img1.size[1]):# #获取图片1的每个坐标 rgb1 = img1.load()[x,y] #获取图片2每个坐标的像素 rgb2 = img2.load()[x,y] #获取像素的绝对值 r = abs(rgb1[0]-rgb2[0]) g = abs(rgb1[1]-rgb2[1]) b = abs(rgb1[2]-rgb2[2]) if not (r<color_num and g<color_num and b< color_num): return x-7 def get_move(distance): distance+=20 #初速度为0 v0 = 0 #加速度列表 a_list = [2,3,4] #时间 t = 0.2 #初始位移 s = 0 #人的滑动轨迹列表 move_list = [] #中间值 mid = distance*0.6 #判断如果位移小于总距离 while s < distance: if s< mid: a = a_list[random.randint(0,2)] else: a = - a_list[random.randint(0,2)] v = v0 #计算位移 move = v0 * t +0.5 * a*(t**2) move = round(move)#去掉小数 #把当前速度替换给初始速度 v0 = v +a*t #替换初始位移 s+=move move_list.append(move) #回退列表 back_list = [-1, -2, -1, -1, -2, -3, -3, -2, -2, -1, -1, -1] print(444444) return {"move_list":move_list,"back_list":back_list} def main(): driver = webdriver.Chrome(chrome_options=option) try: login_url="https://account.cnblogs.com/signin?returnUrl=http%3a%2f%2fhome.cnblogs.com%2fu%2ftangda%2f" driver.get(login_url) driver.implicitly_wait(10) #1输入用户名与密码 并点击登录 user = driver.find_element_by_id("LoginName") pwd = driver.find_element_by_id("Password") user.send_keys("会飞的猫1122") pwd.send_keys("*********") time.sleep(1) button = driver.find_element_by_id("submitBtn") button.click() time.sleep(3) #1获取没有缺口的照片 image1 = get_image1(driver) #2获取有缺口的照片 image2 = get_image2(driver) distance = get_distance(image1,image2) print(distance) #模拟人的滑动轨迹 move_dict = get_move(distance) print(2222222) #前进的轨迹 move_list = move_dict.get("move_list") #后退的轨迹 back_list = move_dict.get("back_list") print(11111) move_button = driver.find_element_by_class_name("geetest_slider_button") ActionChains(driver).click_and_hold(move_button).perform() print(222222) #开始滑动 #前进 for x in move_list: ActionChains(driver).move_by_offset(xoffset=x,yoffset=0).perform() time.sleep(0.1) #后退 for x in back_list: ActionChains(driver).move_by_offset(xoffset=x,yoffset=0).perform() time.sleep(0.1) #虚晃 ActionChains(driver).move_by_offset(xoffset=-3,yoffset=0).perform() ActionChains(driver).move_by_offset(xoffset=3,yoffset=0).perform() #释放动作lian链 ActionChains(driver).release().perform() time.sleep(1000) finally: driver.close() if __name__ == '__main__': main()