selenium页面跳转问题-QQ邮箱登录之后找不到元素

QQ邮箱自动登录可以参考:python实现滑动验证
登录之后需要定位元素“写信":怎么也定位不到,可能报错的地方也已经修改过了,不是iframe和定位位置不正确的问题:[NoSuchElementException]
这时候应该考虑页面跳转:python-selenium窗口切换

但是QQ邮箱的情况不太一样,域名都是mail.qq.com,只是登录之后新增加了一些子级域名,因此打印窗口句柄会发现登陆前后是一样的,只是域名改变了,因此产生错误的原因在于:页面元素没有加载出来,导致无法定位到元素,因此可以通过显式等待来解决

登陆的代码如下:

import time
import paramiko
import pyautogui
import os
from time import sleep
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from Auto_Test.base_action import match
import pyperclip
import random
from Auto_Test.base_action import random_and_write
browser = webdriver.Firefox()



def login_in():
    global browser
    browser.maximize_window()
    browser.get("https://mail.qq.com/")
    browser.switch_to.frame("login_frame")
    browser.find_element_by_class_name("inputstyle").clear()
    browser.find_element_by_class_name("inputstyle").send_keys("3540830376")
    browser.find_element_by_class_name("inputstyle.password").clear()
    browser.find_element_by_class_name("inputstyle.password").send_keys("LUO808089lin")
    browser.find_element_by_id("login_button").click()
    browser.find_element_by_class_name("login_button").click()
    time.sleep(2)
    browser.switch_to.frame("tcaptcha_iframe")

    print("~" * 10)
    titl = browser.title
    print(titl)
    now_url = browser.current_url
    print(now_url)
    print("~" * 10)

    time.sleep(3)
    # 等待图片加载出来
    WebDriverWait(browser, 5, 0.5).until(
        EC.presence_of_element_located((By.ID, "tcaptcha_drag_button")))

    current_window = browser.current_window_handle  # 获取当前窗口handle name
    print(current_window,"####")
    lx,ly=match("QQmail-slick")
    lz=[x for x in range(150,200,3)]
    for i in lz:
        pyautogui.moveTo(lx,ly)
        pyautogui.dragRel(i,0,duration=2,button='left')
        time.sleep(1)
        pyautogui.moveRel(-i,0)

        try:
            alert = browser.find_element_by_id('guideText').text
        except Exception as e:
            print('get alert error: %s' % e)
            alert = ''
        if alert:
            print(u'滑块位移需要调整: %s' % alert)
            sleep(3)
        else:
            print('滑块验证通过')
            browser.switch_to.parent_frame()  # 验证成功后跳回最外层页面
            break
    browser.switch_to.default_content()

发送信息的函数代码:


def send_message():
    global browser
    print("切换之前")
    print("*"*10)
    titl=browser.title
    print(titl)
    now_url=browser.current_url
    print(now_url)
    print("*"*10)
    time.sleep(3)
    current_window = browser.current_window_handle  # 获取当前窗口handle name
    print(current_window,"&&&&&")
    all_window = browser.window_handles
    print(all_window,"&&&")
    print("*"*18)
    browser.switch_to.window(current_window)
    print("切换之后")
    print("$" * 10)
    titl = browser.title
    print(titl)
    now_url = browser.current_url
    print(now_url)
    print("$" * 10)
    current_window = browser.current_window_handle  # 获取当前窗口handle name
    print(current_window, "!!!!")
    all_window = browser.window_handles
    print(all_window, "!!!")
    print("*"*18)

主函数中查找的是登陆之后的页面元素"写信":

if __name__ == '__main__':
    login_in()
    WebDriverWait(browser, 15, 0.5).until(
        EC.presence_of_element_located((By.ID, "composebtn_td")))
    send_message()

运行结果为:


~~~~~~~~~~
登录QQ邮箱
https://mail.qq.com/
~~~~~~~~~~
18 ####
切换之前
**********
登录QQ邮箱
https://mail.qq.com/
**********
18 &&&&&
['18'] &&&
******************
切换之后
$$$$$$$$$$
QQ邮箱
https://mail.qq.com/cgi-bin/frame_html?sid=1n7EDcWwNTlb5TZW&r=f256c0f155b6b5bf0aecf1bb407bbd7f
$$$$$$$$$$
18 !!!!
['18'] !!!
******************

增加等待之后就可以了:
并且跳转窗口的代码都可以去掉了,结果是一样的!

browser.switch_to.window(current_window)

自己的运行得到的结果,如果有不一致的地方,欢迎交流讨论,共同学习!

猜你喜欢

转载自blog.csdn.net/liulanba/article/details/115319492