IFrame nesting applications often occur in Web applications, assuming that there is a and B two iframe on the page, where B is within a, then the content in B will need to go first to a and then to B.
The IFRAME is actually embedded in another page, and Webdriver can only be identified in one page at a time, so you need to use the Switch_to.frame method to get the embedded page in the IFRAME, to locate the elements in that page.
If an IFRAME has ID or name, using Switch_to_frame () can be easily positioned, as
Example 1:
# Find the ifrome1 first (id = F1)
Driver.switch_to_frame ("F1")
# and find the ifrome2 below it (ID =f2)
Driver.switch_to_frame ("F2")
# The following will be the normal operation of the element
driver.find_element_by_id ("XX"). Click ()
Note: After you cut into the frame, we cannot continue to manipulate the elements of the main frame, then if you want to manipulate the main frame content, you need to cut back the main document (the most superior document);
Br.switch_to.default_content ()
But sometimes there is no ID in the IFRAME or the situation, which requires other ways to locate, such as:
Example 2:
Method One: Starting from the top level, relatively laborious.
Text1 = Browser.find_element_by_class_name ("BODYBG")
Text2 = Text1.find_element_by_xpath ("div[@class = ' lean_overlay ']/div[2]")
# Enter the SRC content in the IFRAME to confirm that it has been positioned to the IFRAME
# Text3 = Text2.find_element_by_tag_name ("iframe"). Get_attribute ("src")
# Print Text3
Text = Text2.find_element_by_tag_name ("iframe")
Browser.switch_to.frame (text)
Method Two: Navigate to the previous, and then navigate to the IFRAME.
#找出所有class_name the upper level of the =qh-login element
Text1 = Browser.find_elements_by_class_name ("Qh-login")
For text in Text1:
If Text.get_attribute ("id") = = "Idname":
Print Text.get_attribute ("class")
Text2 = Text.find_element_by_tag_name ("iframe"). Get_attribute ("src")
Else
Text2 = str ("Failed to locate")
--------------------------------------------------------------------------------------------------------------- ------------------
It is possible to nest not frames, but windows, and methods for Windows: Switch_to_window
Usage is the same as Switch_to_frame:
Driver.switch_to_window ("Windowname")
Some pages are inlaid with frame:
Usage is the same as Switch_to_frame:
Driver.switch_to.frame ("id| | Name ")