selenium破解小滑块验证码

今天感觉挺无聊的,就写一篇破解验证码的文章,在我们不管是做自动化还是爬虫的过程中,我们会遇到各种验证码的问题。

今天我们说说破解滑块验证码,拉到最右边的问题。首先我们的实例是这个网站:https://login.aliexpress.com/

输入邮箱 输入密码后 如果你想登入 就需要拖动这个验证码到最右侧 但是呢你又不可以很迅速的向右拖动 不然人家网站会判断你是机器

大概的思路其实很简单:

  第一个 我们定位滑块

  第二步 我们就模拟鼠标按住模块

  第三步 就是拖动了(这里我们要实现随机的移动 一次就移动一点点)

        写一个循环  

        一点点的移动

        直到拖动到最右边

        然后每次移动之后 我们截图保存看当前的移动效果

        最后就可以移动有最右侧了

第一步:打开浏览器 输入邮箱 密码 

from selenium.webdriver.common.action_chains import ActionChains
import time,random
from selenium import webdriver

#配置代理
proxy = "代理ip:端口号"
desired_capabilities = webdriver.DesiredCapabilities.CHROME.copy()
desired_capabilities['proxy'] = {
    "httpProxy":proxy,
    "ftpProxy":proxy,
    "sslProxy":proxy,
    "noProxy":None,
    "proxyType":"MANUAL",
    "class":"org.openqa.selenium.Proxy",
    "autodetect":False
}
#配置useragent 不配置会认为你是机器人哦 你可能可以成功移动最右侧 但是还是会通过不了哦 所以配置ua

desired_capabilities["userAgent"] = (
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
)
driver = webdriver.Remote("http://你的ip地址:4444/wd/hub", desired_capabilities)
driver.get("https://login.aliexpress.com/")
driver.maximize_window()

  

第二步:输入邮箱 密码 (这里有一个注意点 我们要先切近iframe )--这边整块的登入信息都包裹在iframe中

#切 iframe
iframe = driver.find_element_by_xpath("//iframe")
driver.switch_to.frame(0)

driver.find_element_by_css_selector("input#fm-login-id").send_keys("邮箱")
driver.find_element_by_css_selector("input#fm-login-password").send_keys("密码")

  

第三步:破解验证码(一点一点的移动,随机延迟[0-2]秒),以下是代码还有截图信息

try:
    huakuai = driver.find_element_by_css_selector("span#nc_1_n1z")
    print("找到滑块元素")
except:
    print("没有找到滑块元素")
    pass
else:
    action = ActionChains(driver)
    action.click_and_hold(huakuai).perform()
  #循环的次数自己控制 for index in range(18): print("index:%d" %index) try: action.move_by_offset(2, 0).perform() # 平行移动鼠标 driver.save_screenshot('login-screeshot-i-' + str(index) + '.png') except Exception as e: print(e) break if (index == 17): action.release() time.sleep(1) driver.save_screenshot('login-screeshot-i-' + str(index) + '.png') else: sleeptime = random.randint(0,2) print("延迟%d秒" %sleeptime) time.sleep(sleeptime) print("滑块移动完成")

 下面是每次的截图信息 运行过程:

   

以上就是一个破解验证码的过程!

其实大家都会破解这个验证码,并不难,重要是我们怎么让他无规律一些,模拟人为的操作,而不是被认为是机器!

猜你喜欢

转载自www.cnblogs.com/zhe-hello/p/10345073.html