python自动化操作浏览器 识别滑动验证

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= 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)