滑块验证,模拟登录

  通过QQ邮箱来进行测试,滑块验证,selenium 模拟登录,保存登录后的Cookies;利用Cookies,登录QQ邮箱,但返回的网页内容不太对…

1. 保存 Cookies

# -*- coding: utf8 -*-
from selenium.webdriver import ActionChains
from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException
import time
import json

def save_cookie(url,username,password):

    # selenium + webdriver 被 QQ邮箱识别,解决:
    driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        "source": """
        Object.defineProperty(navigator, 'webdriver', {
           get: () => undefined
         })
       """
    })

    driver.get(url)
    driver.switch_to.frame("login_frame")     # 进入frame,不然定位不到标签位置
    driver.find_element_by_xpath('//*[@id="u"]').send_keys(username)  # 定位账号,并输入账户username
    driver.find_element_by_xpath('//*[@id="p"]').send_keys(password)  # 定位密码,并输入密码password
    time.sleep(1)
    driver.find_element_by_xpath('//*[@id="login_button"]').click()   # 定位确定按钮,并点击
    time.sleep(1)

    driver.switch_to.frame('tcaptcha_iframe')   # 进入frame,不然定位不到标签位置
    start_position = driver.find_element_by_id('tcaptcha_drag_button')  # 定位初始滑块
    time.sleep(2)
    action = ActionChains(driver)
    action.click_and_hold(start_position).perform()  # 鼠标移动到“滑块”初始位置,并保持点击不释放
    action.reset_actions()  # 清除已经存储的操作

    # 先水平向右“滑块” 120,因为QQ邮箱验证图片缺失部分是靠右的,根据经验先移动120,靠近目标位置
    action.move_by_offset(xoffset=120, yoffset=0).perform()
    time.sleep(1)

    # 再每次移动距离 +10,即 130、140、150...直至差不多移动到目标位置
    try:
        for i in range(10): # 主观判断最多移动10次,即可匹配到目标位置
            action.move_by_offset(xoffset=10,yoffset=0).perform()
            action.reset_actions()
            time.sleep(1)
    except StaleElementReferenceException:  # 到了目标位置,验证通过,会刷新页面
        time.sleep(5)

    # 保存 Cookies
    cookies = driver.get_cookies()
    # return cookies
    time.sleep(3)
    with open("cookies.txt","w") as f:
        json.dump(cookies,f)

if __name__ == '__main__':
    driver = webdriver.Chrome()
    url = "https://mail.qq.com/"
    username = '×××'
    password = '×××'
    cookies = save_cookie(url,username,password)

2. Cookies 登录

# -*- coding: utf8 -*-
import urllib3
import requests
import json
from requests.cookies import RequestsCookieJar

# 忽略控制台 InsecureRequestWarning
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

s = requests.session()
s.verify = False
s.headers = {
    "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"
}
s.get("https://mail.qq.com/")

# 读取并设置 Cookies
jar = RequestsCookieJar()
with open("cookies.txt", "r") as fp:
    cookies = json.load(fp)
    for cookie in cookies:
        jar.set(cookie['name'], cookie['value'])

url = 'https://mail.qq.com/cgi-bin/frame_html?sid=Y9q2mxBWsaX1QmwM&r=68eb4bbf74115c7246abeac47d3fc5ad'
response = s.get(url, cookies=jar)
html = response.text
print(html)

3. 遇到的Bug

  (1) 账号、密码、滑块位置都正确的情况下,python+selenium被限制登录QQ邮箱,解决
  (2) 报错Message: stale element reference: element is not attached to the page docume,解决
  (3) 控制台 InsecureRequestWarning,解决

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值