First, describe the problem:
automatic QQ mailbox login, after the account password is entered, it jumps to the verification interface. At this time, the element needs to be located but it can't be located: If
there is a problem with the account password input, please refer to: the problem of unable to locate the element The
code is as follows:
browser = webdriver.Firefox()
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("xxxx")
browser.find_element_by_class_name("inputstyle.password").clear()
browser.find_element_by_class_name("inputstyle.password").send_keys("xxxx")
browser.find_element_by_id("login_button").click()
browser.find_element_by_class_name("login_button").click()
# browser.switch_to.default_content() # 回到默认的iframe
# 等待资源加载
time.sleep(3)
# 等待图片加载出来
browser.find_element_by_id('tcaptcha_drag_button').click()
The page elements are as follows:
but an error is reported: NoSuchElementException
Look up again and find that the iframe closest to the element is another.
Modify the code, first return to the default iframe, and then to the new iframe:
browser = webdriver.Firefox()
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("xxxx")
browser.find_element_by_class_name("inputstyle.password").clear()
browser.find_element_by_class_name("inputstyle.password").send_keys("xxxx")
browser.find_element_by_id("login_button").click()
browser.find_element_by_class_name("login_button").click()
browser.switch_to.default_content() # 回到默认的iframe
# 等待资源加载
time.sleep(3)
browser.switch_to.frame("tcaptcha_iframe")
browser.find_element_by_id('tcaptcha_drag_button').click()
Will report an error: NoSuchFrameException
This will blow up! Take a break and bite the bullet and check the iframe:
First look at the iframe: the
iframe tag specifies an inline frame.
An inline frame is used to embed another document in the current HTML document.
Parallel nesting :
Hierarchical nesting :
This can be understood by looking at it: tcaptcha_iframe is nested in login_frame, so the above code does not need to go back to the default iframe, just go to another layer:
browser = webdriver.Firefox()
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("xxxx")
browser.find_element_by_class_name("inputstyle.password").clear()
browser.find_element_by_class_name("inputstyle.password").send_keys("xxxx")
browser.find_element_by_id("login_button").click()
browser.find_element_by_class_name("login_button").click()
#browser.switch_to.default_content() # 回到默认的iframe
# 等待资源加载
time.sleep(3)
browser.switch_to.frame("tcaptcha_iframe")
browser.find_element_by_id('tcaptcha_drag_button').click()
Successfully run! Go to the next step!