使用selenium识别滑动验证码(二)

上一篇文章是在开发者模式下驱动浏览器,在网上也看到了不同的方法,我验证了下,在此附上。我看到的是添加mitmdump代理驱动浏览器。
技术参考来源:https://zhuanlan.zhihu.com/p/43581988
https://blog.csdn.net/hihell/article/details/88648151
如果想了解原理可以参考我附上的链接。
按照要求首先修改mitmdump源码
新建一个py文件indject_js_proxy.py

from mitmproxy import ctx

injected_javascript = '''
// overwrite the `languages` property to use a custom getter
Object.defineProperty(navigator, "languages", {
  get: function() {
    return ["zh-CN","zh","zh-TW","en-US","en"];
  }
});
// Overwrite the `plugins` property to use a custom getter.
Object.defineProperty(navigator, 'plugins', {
  get: () => [1, 2, 3, 4, 5],
});
// Pass the Webdriver test
Object.defineProperty(navigator, 'webdriver', {
  get: () => false,
});
// Pass the Chrome Test.
// We can mock this in as much depth as we need for the test.
window.navigator.chrome = {
  runtime: {},
  // etc.
};
// Pass the Permissions Test.
const originalQuery = window.navigator.permissions.query;
window.navigator.permissions.query = (parameters) => (
  parameters.name === 'notifications' ?
    Promise.resolve({ state: Notification.permission }) :
    originalQuery(parameters)
);
'''
def response(flow):
    # Only process 200 responses of HTML content.
    if not flow.response.status_code == 200:
        return

    # Inject a script tag containing the JavaScript.
    html = flow.response.text
    html = html.replace('', '' % injected_javascript)
    flow.response.text = str(html)
    ctx.log.info('>>>> js代码插入成功 <<<<')


在文件的目录下运行此文件
在这里插入图片描述
再新建一个py文件用selenium驱动浏览器访问这个网站https://intoli.com/blog/not-possible-to-block-chrome-headless/chrome-headless-test.html验证添加到mitmdump的代理是否有效。
使用selenium识别滑动验证码(二)_第1张图片
结果都为绿色的即代理添加成功。
上面做完了,就可以写出完整的代码,滑动滑块。

from selenium import webdriver
import time
from selenium.webdriver import ChromeOptions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import ActionChains
import random
browser = webdriver.Chrome()
wait = WebDriverWait(browser, 40)
items =[]
def get_track(distance ):
    track = []
    current = 0
    mid = distance * (3/5)
    t = 3
    v = 0
    v1  = 0
    a1 = 4
    a = 8
    while current < distance:
        if current < mid:
            move = v * t + 1/2 * a *t * t
            current += move
            track.append(round(move))
            v = v + a*t
        else:
            move1 = v1 * t + 1/2 * a1 *t *t
            current += move1
            track.append(round(move1))
            v1 = v1 + a1*t
    return track
def huadong():
    slider = browser.find_element_by_xpath("//*[@id='nc_2_n1z']")  # 找到滑动按钮
    ActionChains(browser).click_and_hold(slider).perform()
    track = get_track(700)  # 模拟运动轨迹,速度先快后慢
    for x in track:
        ActionChains(browser).drag_and_drop_by_offset(slider, xoffset=x, yoffset=random.randint(1, 3)).perform()
    ActionChains(browser).release().perform()
def login():
    browser.get('https://promotion.aliyun.com/ntms/act/captchaIntroAndDemo.html')
    time.sleep(10)
    huadong1 = browser.find_element_by_xpath('//*[@id="tab2"]/a')
    huadong1.click()
    huadong()
def main():
    login()
    time.sleep(10)
    browser.close()
if __name__ == '__main__':
    main()


同样地在此selenium下打开控制台查看
在这里插入图片描述
使用selenium识别滑动验证码(二)_第2张图片

你可能感兴趣的:(使用selenium识别滑动验证码(二))