selenium.common.exceptions.NoSuchElementException: Message: Unable to locate:xxx

Recently I am engaged in automated testing, using the selenium framework, and getting the id, class, etc. of the elements in the page review, but when using the find_element_by* of Selenium WebDrive to locate the elements, an error will be reported:
Insert picture description here

code show as below:

#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import paramiko
import pyautogui
browser=webdriver.Firefox()
browser.get("https://mail.qq.com/")
browser.find_element_by_class_name("inputstyle").send_keys("xxxxxxx")
pyautogui.hotkey('Tab')

raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: Unable to
locate element: .inputstyle

Operations such as increasing the delay have been tried, but it is useless!
Looking for another method, I saw that it is because: an Iframe is nested inside.
When jumping to the "security verification" page, a new frame is "entered", which can be understood as embedding in the "login page" A "verification page" is set, and all the html elements loaded by the current browser are "login pages". If you want to find this element, you must first switch to the "verification page"
Insert picture description here. You need to switch when you encounter this type of problem. Once, switch to the embedded Iframe to be able to locate it.
Modify the code:

browser=webdriver.Firefox()
browser.get("https://mail.qq.com/")
browser.switch_to.frame("login_frame")#切换到内嵌iframe
browser.find_element_by_class_name("inputstyle").send_keys("xxxxx")
pyautogui.hotkey('Tab')
browser.find_element_by_class_name("inputstyle.password").send_keys("xxxxx")
browser.switch_to.default_content()#回到默认的iframe

Successfully run!
Questions about iframe: reference link

Guess you like

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