• selenium模板实现实现滑动验证


    1心得体会

    之前还没有听说过selenium模板,现在发现这个模板挺牛逼的。能自动模拟用户操作浏览器,不过缺点就是慢了写,但是不需要自己写cookie、headers这些了,毕竟是用真实的浏览器去模板。主要是好不是很熟练,还需要多敲代码。

    2、selenium介绍

    selenium是一款自动化测试工具,支持很多主流的浏览器。只要浏览器安装了依赖驱动就行。

    下面代码是模拟访问百度操作

    1 from selenium import webdriver
    2 driver = webdriver.Chrome()
    3 driver.get("https://www.baidu.com")

    只需简单的代码就可以让浏览器自己访问百度了。当然,这只是简单的操作。

    对于用来操作来说,还有输入内容和点击这两个常用的功能

    test = driver.find_element_by_xpath(xpath)这个语句可以根据xpath来找到相应的位置.

    如果可以点击的话,test.click()  就会自己点击。

    如果是需要输入内容的话,test.send_keys()

    3.模拟登陆 https://dig.chouti.com并点赞

    1、先配置驱动

    1 # 配置驱动
    2 option = webdriver.ChromeOptions()
    3 # driver = webdriver.Chrome('/Users/wupeiqi/drivers/chromedriver', chrome_options=option)
    4 driver = webdriver.Chrome( chrome_options=option)

    2、找到登陆按钮并点击

    1 # 1. 控制浏览器打开指定页面
    2 driver.get("https://dig.chouti.com")
    3 
    4 
    5 # 2. 找到登录按钮
    6 btn_login = driver.find_element_by_xpath('//*[@id="login-link-a"]')
    7 # 3. 点击按钮
    8 btn_login.click()

    3、输入账号和密码

    1 # 4. 找到手机标签
    2 input_user = driver.find_element_by_xpath('//*[@id="mobile"]')
    3 # 5. 找到密码标签
    4 input_pwd = driver.find_element_by_xpath('//*[@id="mbpwd"]')
    5 
    6 # 6. 输入用户名
    7 input_user.send_keys('18879970712')
    8 # 7. 输入密码
    9 input_pwd.send_keys('lg970712')

    4、点击登陆按钮

    1 # 8. 点击登录按钮
    2 input_submit = driver.find_element_by_xpath(
    3     '//*[@id="footer-band"]/div[5]/div/div/div[1]/div[2]/div[4]/div[2]/div/span[1]')
    4 input_submit.click()

    5、找到要点赞的新闻并点赞

    1 # 9. 点击跳转
    2 news = driver.find_element_by_xpath('//*[@id="newsContent20696066"]/div[2]/a[1]')
    3 news.click()
    4 driver.execute_script("arguments[0].click();", news)

    6、最后关闭驱动

    1 # 10.关闭浏览器
    2 driver.close()

    4、滑动验证

    实现滑动验证之前的步骤和前面的一样。登陆网站,找到输入账号和密码的位置输入账号和密码。不过验证的时候就需要滑动验证的,这就需要处理图片了。滑动验证一开始有一张完整的图片,将完整的图片下载下来,然后还有一张不完整的图片,也下载下来。我们需要做的就是对比着两张图片哪里的像素不同,根据不同像素的位置来计算需要滑动多少像素,然后去模拟浏览器去滑动。

    完整代码如下:

      1 from selenium import webdriver
      2 from selenium.webdriver import ActionChains
      3 from selenium.webdriver.common.by import By
      4 from selenium.webdriver.common.keys import Keys
      5 from selenium.webdriver.support import expected_conditions as EC
      6 from selenium.webdriver.support.wait import WebDriverWait
      7 import os
      8 import shutil
      9 from PIL import Image
     10 import time
     11 
     12 
     13 def get_snap(driver):
     14     driver.save_screenshot('full_snap.png')
     15     page_snap_obj = Image.open('full_snap.png')
     16 
     17     return page_snap_obj
     18 
     19 
     20 def get_image(driver):
     21     img = driver.find_element_by_class_name('geetest_canvas_img')
     22     time.sleep(2)
     23     location = img.location
     24     size = img.size
     25 
     26     left = location['x']
     27     top = location['y']
     28     right = left + size['width']
     29     bottom = top + size['height']
     30 
     31     page_snap_obj = get_snap(driver)
     32 
     33     image_obj = page_snap_obj.crop((left, top, right, bottom))
     34     # image_obj.show()
     35     with open('code.png', 'wb') as f:
     36         image_obj.save(f, format='png')
     37     return image_obj
     38 
     39 
     40 def get_distance(image1, image2):
     41     # start = 0
     42     # threhold = 70
     43     # for i in range(start, image1.size[0]):
     44     #     for j in range(0, image1.size[1]):
     45     #         rgb1 = image1.load()[i, j]
     46     #         rgb2 = image2.load()[i, j]
     47     #         res1 = abs(rgb1[0] - rgb2[0])
     48     #         res2 = abs(rgb1[1] - rgb2[1])
     49     #         res3 = abs(rgb1[2] - rgb2[2])
     50     #         # print(res1,res2,res3)
     51     #         if not (res1 < threhold and res2 < threhold and res3 < threhold):
     52     #             print(111111, i, j)
     53     #             return i - 13
     54     # print(2222, i, j)
     55     # return i - 13
     56     start = 0
     57     threhold = 70
     58     v = []
     59     for i in range(start, image1.size[0]):
     60         for j in range(0, image1.size[1]):
     61             rgb1 = image1.load()[i, j]
     62             rgb2 = image2.load()[i, j]
     63             res1 = abs(rgb1[0] - rgb2[0])
     64             res2 = abs(rgb1[1] - rgb2[1])
     65             res3 = abs(rgb1[2] - rgb2[2])
     66 
     67             if not (res1 < threhold and res2 < threhold and res3 < threhold):
     68                 print(i)
     69                 if i not in v:
     70                     v.append(i)
     71 
     72     stop = 0
     73     for i in range(0, len(v)):
     74         val = i + v[0]
     75         if v[i] != val:
     76             stop = v[i]
     77             break
     78 
     79     width = stop - v[0]
     80     print(stop, v[0], width)
     81     return width
     82 
     83 
     84 def get_tracks(distance):
     85     import random
     86     exceed_distance = random.randint(0, 5)
     87     distance += exceed_distance  # 先滑过一点,最后再反着滑动回来
     88     v = 0
     89     t = 0.2
     90     forward_tracks = []
     91 
     92     current = 0
     93     mid = distance * 3 / 5
     94     while current < distance:
     95         if current < mid:
     96             a = random.randint(1, 3)
     97         else:
     98             a = random.randint(1, 3)
     99             a = -a
    100         s = v * t + 0.5 * a * (t ** 2)
    101         v = v + a * t
    102         current += s
    103         forward_tracks.append(round(s))
    104 
    105     # 反着滑动到准确位置
    106     v = 0
    107     t = 0.2
    108     back_tracks = []
    109 
    110     current = 0
    111     mid = distance * 4 / 5
    112     while abs(current) < exceed_distance:
    113         if current < mid:
    114             a = random.randint(1, 3)
    115         else:
    116             a = random.randint(-3, -5)
    117             a = -a
    118         s = -v * t - 0.5 * a * (t ** 2)
    119         v = v + a * t
    120         current += s
    121         back_tracks.append(round(s))
    122     return {'forward_tracks': forward_tracks, 'back_tracks': list(reversed(back_tracks))}
    123 
    124 
    125 def crack(driver):  # 破解滑动认证
    126     # 1、点击按钮,得到没有缺口的图片
    127     button = driver.find_element_by_xpath('//*[@id="embed-captcha"]/div/div[2]/div[1]/div[3]')
    128     button.click()
    129 
    130     # 2、获取没有缺口的图片
    131     image1 = get_image(driver)
    132 
    133     # 3、点击滑动按钮,得到有缺口的图片
    134     button = driver.find_element_by_class_name('geetest_slider_button')
    135     button.click()
    136 
    137     # 4、获取有缺口的图片
    138     image2 = get_image(driver)
    139 
    140     # 5、对比两种图片的像素点,找出位移
    141     distance = get_distance(image1, image2)
    142     print(distance)
    143     #
    144     # 6、模拟人的行为习惯,根据总位移得到行为轨迹
    145     tracks = get_tracks(int(distance / 2))
    146 
    147     # 7、按照行动轨迹先正向滑动,后反滑动
    148     button = driver.find_element_by_class_name('geetest_slider_button')
    149     ActionChains(driver).click_and_hold(button).perform()
    150 
    151     # 正常人类总是自信满满地开始正向滑动,自信地表现是疯狂加速
    152     for track in tracks['forward_tracks']:
    153         ActionChains(driver).move_by_offset(xoffset=track, yoffset=0).perform()
    154 
    155     # 结果傻逼了,正常的人类停顿了一下,回过神来发现,卧槽,滑过了,然后开始反向滑动
    156     time.sleep(0.5)
    157     for back_track in tracks['back_tracks']:
    158         ActionChains(driver).move_by_offset(xoffset=back_track, yoffset=0).perform()
    159     #
    160     # # 小范围震荡一下,进一步迷惑极验后台,这一步可以极大地提高成功率
    161     ActionChains(driver).move_by_offset(xoffset=3, yoffset=0).perform()
    162     ActionChains(driver).move_by_offset(xoffset=-3, yoffset=0).perform()
    163 
    164     # # 成功后,骚包人类总喜欢默默地欣赏一下自己拼图的成果,然后恋恋不舍地松开那只脏手
    165     time.sleep(0.5)
    166     ActionChains(driver).release().perform()
    167 
    168 
    169 def login_luffy(username, password):
    170     driver = webdriver.Chrome()
    171     driver.set_window_size(960, 800)
    172     try:
    173         # 1、输入账号密码回车
    174         driver.implicitly_wait(3)
    175         driver.get('https://www.luffycity.com/login')
    176         input_username = driver.find_element_by_xpath('//*[@id="router-view"]/div/div/div[2]/div[2]/input[1]')
    177         input_pwd = driver.find_element_by_xpath('//*[@id="router-view"]/div/div/div[2]/div[2]/input[2]')
    178 
    179         input_username.send_keys(username)
    180         input_pwd.send_keys(password)
    181 
    182         # 2、破解滑动认证
    183         crack(driver)
    184 
    185         time.sleep(10)  # 睡时间长一点,确定登录成功
    186     finally:
    187         pass
    188         # driver.close()
    189 
    190 
    191 if __name__ == '__main__':
    192     login_luffy(username='wupeiqi', password='123123123')
    破解滑动验证
  • 相关阅读:
    如何在Root的手机上开启ViewServer,使得HierachyViewer能够连接
    java调用monkeyrunner(亲测绝对可行)
    如何在DOS窗口复制和粘贴命令
    查看当前APP打开的是哪个Activity
    MonkeyRunner之MonkeyRecorder录制回放脚本(亲测可正常运行)
    selenium python2.7安装配置
    常用adb操作命令详解
    Spring整合strus2简单应用总结
    spring整合strus2的Hellowworld
    责任链模式-对象行为型
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/9280983.html
Copyright © 2020-2023  润新知