004python+selenium+opencv滑块验证

一.内容简介:

  实现对QQ自动模拟登录,以及解决滑块验证。

二.模块描述:

  (1).qq_login.py:主程序
  (2).test_distance.py:定位滑块,获取x轴方向
  (3).info.json:个人账户(json数据格式)

三.实现:

  (1)分析url:https://i.qq.com/;F12调试打开
  (2)问题一:关于iframe框架作用域问题,会影响接下来定位不到元素。
  (3)问题二:关于iframe多层跳入与跳出问题,会影响接下来定位不到滑块元素。


代码一:

import os
import re
import sys
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
import time
import json
from parsel import Selector
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from test_distance import get_distance


class QQLogin(object):
    def __init__(self):
        self.bw = webdriver.Chrome()

    def get_qq_page(self, url):
    	# 创建brower对象
        bw = self.bw
        # 全窗口显示
        bw.maximize_window()
        bw.get(url)
        # 跳过作用域
        bw.switch_to.frame('login_frame')
        # page = bw.page_source
        # 定位选择使用账号密码登录
        swither_login = WebDriverWait(bw, 10, 0.5).until(EC.presence_of_element_located((By.ID, "switcher_plogin")))
        swither_login.click()
        # 获取个人用户信息(账号,密码)
        with open('info.json', 'r', encoding="utf-8") as fr:
            account = json.load(fr)
        # 定位账号输入框
        user_area = WebDriverWait(bw, 5, 0.5).until(EC.presence_of_element_located((By.ID, "u")))
        user_area.click()
        bw.find_element_by_id('u').send_keys(account["username"])
        # 定位密码输入框
        pwd_area = WebDriverWait(bw, 5, 0.5).until(EC.presence_of_element_located((By.ID, "p")))
        pwd_area.click()
        bw.find_element_by_id('p').send_keys(account["pwd"])
        # 点击登录
        login_submit = WebDriverWait(bw, 5

你可能感兴趣的:(反爬虫原理,selenium,opencv)