Selenium page jump problem-element cannot be found after QQ mailbox login

For QQ mailbox automatic login, please refer to: Python implements sliding verification.
After logging in, you need to locate the element "write letter": it can't be located anyway, and the place where the error may be reported has also been modified. It is not the problem of iframe and incorrect positioning position: [NoSuchElementException]
At this time, you should consider page jump: python-selenium window switch

But the situation of QQ mailbox is different. The domain name is mail.qq.com, but some sub-domains are added after login. Therefore, the print window handle will find that it is the same before and after login, but the domain name has changed, so an error occurs. The reason is: the page elements are not loaded, resulting in the inability to locate the element, so it can be solved by explicitly waiting

The login code is as follows:

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()

Function code for sending information:


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)

What is found in the main function is the page element "write letter" after login:

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

The results of the operation are:


~~~~~~~~~~
登录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'] !!!
******************

After the wait is added, it is enough:
and the code of the jump window can be removed, and the result is the same!

browser.switch_to.window(current_window)

If there are any inconsistencies in the results of your own operation, please exchange and discuss and learn together!

Guess you like

Origin blog.csdn.net/liulanba/article/details/115319492