iframe: a page nested within another frame / page, i.e. an HTML page to another are also embedded in the HTML page, but this is embedded in HTML </ frame> </ iframe> tag pairs .
In python3.8 corresponding selenium it provides two ways to get the content of the iframe:
方式一:driver.switvh_to.frame(frame_reference)
grammar:
driver.switch_to.frame (iframe's name or an object property or webelement subscript)
Example:
driver.switch_to.frame ( "login_frame_qq") # login_frame_qq name is switched to an iframe
driver.switvh_to.frame (0) # switches to the first an iframe
driver.switch_to.frame ((By.xpath, "// div [@ class = "ptlogin_wrap"] ))
方式二:frame_to_be_available_and_switch_to_it(frame_reference)
In front of the wait operation of the common operations selenium , we have introduced, provided the method expected_conditons module.
This method determines iframe is available, and will automatically switch to the iframe.
frame_reference value with a consistent manner.
Example:
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it(iframe_name))
When there is a page in an iframe iframe when, if at this time we would like to enter the secondary iframe, you'll need to enter an iframe, and then enter the secondary iframe.
#iframeId is one of the iframe id driver.switch_to_frame ( "iframeId") has two parallel under #iframeId iframe, but they do not have id and name, then we can get through tag_name driver.switch_to.frame (driver.find_elements_by_tag_name ( "iframe") [1])
So we enter the next level iframeId the iframe!
selenium out of the iframe
① a jump from two iframe iframe, i.e. Skip Parent:
. driver.switchTo () parentFrame ();
#, or
driver.switch_to.parent_frame()
② Skip to main window from iframe
driver.switch_to_default_content()
#或者
driver.switch_to.default_content()