问题:模拟qq自动登录时候截不到验证码图片

- 超级鹰
    -注册:普通用户
    -登录:普通用户
        -题分查询:充值
        - 创建一个软件(id)
        - 下载实例代码
        - 下载核心代码

利用超级鹰进行图片验证的模拟登录

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from time import sleep
from PIL import Image
option = Options()
option.add_experimental_option("detach", True)
web_driver = webdriver.Chrome()
web_driver.get("https://qzone.qq.com/")
f = web_driver.find_element(By.XPATH, '//*[@id="login_div"]')
print(f.text)
f_range=(
    f.location['x'],f.location['y'],f.location['x']+f.size['width'],f.location['y']+f.size['height']
)
#切换作用域
frame1 = web_driver.find_element(By.ID, 'login_frame')
print(frame1.location)
print(frame1.size)
range1=(
    frame1.location['x'],frame1.location['y'],frame1.location['x']+frame1.size['width'],frame1.location['y']+frame1.size['height']
)
web_driver.switch_to.frame(frame1)
#标签定位
a_tag = web_driver.find_element(By.ID, "switcher_plogin")
a_tag.click()
sleep(5)
username_tag = web_driver.find_element(By.ID, "u")
password_tag = web_driver.find_element(By.ID, "p")
username_tag.send_keys("1870388299")
sleep(2)
password_tag.send_keys("wwsa20030309")
sleep(2)
login_btn = web_driver.find_element(By.ID, "login_button")
sleep(2)
login_btn.click()
sleep(10)
web_driver.save_screenshot('./qq_login.png')


image1 = Image.open('./qq_login.png')
print(f_range)
image__crop = image1.crop(f_range)
image__crop.save('./qq_login_crop.png')
#确定验证码图片的左上角和右下角
# sleep(10)
# range=()
# try:
#     # 等待直到元素出现
#     iframe = web_driver.find_element(By.ID,'tcaptcha_iframe_dy')
#     print(iframe.location)
#     web_driver.switch_to.frame(iframe)
#     element = WebDriverWait(web_driver, 10).until(
#         EC.presence_of_element_located((By.XPATH, '//*[@id="bodyWrap"]'))
#     )
#
#     location = element.location
#     size = element.size
#     range =(
#         location['x'],location['y'],location['x']+size['width'],location['y']+size['height']
#     )
# except Exception as e:
#     print("未能找到元素:", e)
# print(range)
# i = Image.open('./qq_login_crop.png')
# code_img_name = 'code.png'
# frame2 = i.crop(range)
# frame2.save(code_img_name)

我在截取屏幕的时候一直截不到验证码那张图片 可能是因为 没有找对位置

posted @ 2024-03-01 21:54  会秃头的小白  阅读(3)  评论(0编辑  收藏  举报