import os from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from time import sleep import urllib.request import cv2 import numpy as np import xlrd from xlutils.copy import copy from selenium.common.exceptions import NoSuchElementException #获取验证码中的图片 def get_image(driver): image1 = driver.find_element_by_id('slideBg').get_attribute('src') image2 = driver.find_element_by_id('slideBlock').get_attribute('src') req=urllib.request.Request(image1) bkg=open('slide_bkg.png','wb+') bkg.write(urllib.request.urlopen(req).read()) bkg.close() req = urllib.request.Request(image2) blk = open('slide_block.png', 'wb+') blk.write(urllib.request.urlopen(req).read()) blk.close() return 'slide_bkg.png','slide_block.png' #这个是用来模拟人为拖动滑块行为,快到缺口位置时,减缓拖动的速度,服务器就是根据这个来判断是否是人为登录的。 def get_tracks(dis): v=0 t=1 #保存0.3内的位移 tracks=[] current=0 mid=distance*4/5 while current<=dis: if current<mid: a=2 else: a=-3 v0=v s=v0*t+0.5*a*(t**2) current+=s tracks.append(round(s)) v=v0+a*t return tracks #计算缺口的位置,由于缺口位置查找偶尔会出现找不准的现象,这里进行判断,如果查找的缺口位置x坐标小于450,我们进行刷新验证码操作,重新计算缺口位置,知道满足条件位置。(设置为450的原因是因为缺口出现位置的x坐标都大于450) def get_distance(bkg,blk): block = cv2.imread(blk, 0) template = cv2.imread(bkg, 0) cv2.imwrite('template.jpg', template) cv2.imwrite('block.jpg', block) block = cv2.imread('block.jpg') block = cv2.cvtColor(block, cv2.COLOR_BGR2GRAY) block = abs(255 - block) cv2.imwrite('block.jpg', block) block = cv2.imread('block.jpg') template = cv2.imread('template.jpg') result = cv2.matchTemplate(block,template,cv2.TM_CCOEFF_NORMED) x, y = np.unravel_index(result.argmax(),result.shape) #这里就是下图中的绿色框框 cv2.rectangle(template, (y+20, x+20), (y + 136-25, x + 136-25), (7, 249, 151), 2) #之所以加20的原因是滑块的四周存在白色填充 print('x坐标为:%d'%(y+20)) if y+20<450: elem=driver.find_element_by_xpath('//*[@id="reload"]/div') sleep(1) elem.click() bkg,blk=get_image(driver) y,template=get_distance(bkg,blk) return y,template def getDriver(driver): driver.get("") # 窗口最大化 # driver.maximize_window() print(driver.current_url) driver.switch_to.default_content() # 需要重新进入,此命令为退出到最外层 driver.switch_to.frame(driver.find_element_by_xpath("//iframe[@id='contentpage']")) driver.switch_to.frame(driver.find_element_by_xpath("//frame[@id='leftFrame']")) # print("----", driver.page_source) driver.find_element_by_link_text('').click() sleep(1) return driver if __name__ == '__main__': # 第一次使用Chrome() 新建浏览器会话 print(os.getcwd()) driver = webdriver.Chrome(executable_path=os.getcwd()+'\\chromedriver.exe') # 创建一个 Chrome WebDriver 实例 bb = 2 while bb!=1: bb = 1 driver = getDriver(driver) # 读取excl # while 1: # re = input("请输入表格名 例:") # if re != "": # re = os.getcwd()+"\\"+re # break re = os.getcwd() + "\\" + "-.xlsx" wb = xlrd.open_workbook(re) # 打开Excel文件 sheet = wb.sheet_by_name('Sheet1') # 通过excel表格名称(rank)获取工作表 dat = [] # 创建空list print(sheet.nrows - 1) workbooknew = copy(wb) # 复制整个excel内容 ws = workbooknew.get_sheet(0) # 获取第一张表单 for a in range(1, sheet.nrows): # if a >= 3: # break cells = sheet.row_values(a) # 每行数据赋值给cells data = cells[0] # 因为表内可能存在多列数据,0代表第一列数据,1代表第二列,以此类推 print(cells[3]," ",cells[5], " ", cells[4]," ",cells[11]) if cells[11] != "": continue try: driver.switch_to.default_content() # 需要重新进入,此命令为退出到最外层 driver.switch_to.frame(driver.find_element_by_xpath("//iframe[@id='contentpage']")) driver.switch_to.frame(driver.find_element_by_xpath("//frame[@id='mainFrame']")) sleep(0.5) driver.find_element_by_id('ksh').send_keys(cells[5]) sleep(1) driver.find_element_by_id('sfzh').send_keys(cells[4]) driver.find_element_by_id('TencentCaptcha').click() except: print("frameerroe") bb = 2 break # WebDriverWait(driver, 15).until(By.visibility_of_element_located((By.XPATH, '//span[@id="username"]'))) # 滑块 sleep(5) # driver.switch_to_frame(1) while 1: driver.switch_to.default_content() # 需要重新进入,此命令为退出到最外层 driver.switch_to.frame(driver.find_element_by_xpath("//iframe[@id='contentpage']")) driver.switch_to.frame(driver.find_element_by_xpath("//frame[@id='mainFrame']")) driver.switch_to_frame(1) sleep(1) try: bkg, blk = get_image(driver) distance, template = get_distance(bkg, blk) double_distance = int((distance - 70 + 20) / 2) tracks = get_tracks(double_distance) # 由于计算机计算的误差,导致模拟人类行为时,会出现分布移动总和大于真实距离,这里就把这个差添加到tracks中,也就是最后进行一步左移。 tracks.append(-(sum(tracks) - double_distance)) element = driver.find_element_by_id('tcaptcha_drag_thumb') ActionChains(driver).click_and_hold(on_element=element).perform() for track in tracks: ActionChains(driver).move_by_offset(xoffset=track, yoffset=0).perform() sleep(1) ActionChains(driver).release(on_element=element).perform() except: sleep(2) # 点击查询 sleep(1) driver.switch_to.default_content() # 需要重新进入,此命令为退出到最外层 driver.switch_to.frame(driver.find_element_by_xpath("//iframe[@id='contentpage']")) driver.switch_to.frame(driver.find_element_by_xpath("//frame[@id='mainFrame']")) try: driver.find_element_by_id('QueryBtn').click() break except: print("--") sleep(1) try: td = driver.find_elements_by_class_name("tdXX") except NoSuchElementException: sleep(2) print("tdXX") continue try: print(cells[3]," ",td[6].text) ws.write(a, 11, td[6].text) # 第一行,第四列,改写为changed! except: print("error") ws.write(a, 11, "-1000") # 第一行,第四列,改写为changed! workbooknew.save(re) # 保存第一张表单 # print("保存中") # workbooknew.save(r'') # 保存第一张表单 re = input("结束") driver.quit()
python自动化操作浏览器 识别滑动验证
最新推荐文章于 2023-10-14 17:42:11 发布