A, iframe elements
1, iframe element: iframe is an HTML tag, which is a document in the document, or floating frame (FRAME). iframe element is created that contains an inline frame (ie a row within the framework of) another document.
- "appears when using find_element_by_class_name other issues not identified, the operating range is the current html, inline content does not contain html documents inside.
- "To operate the element which must switch operating range to be embedded in the document. ( Iframes. Internal )
Switching manner: using switch_to object attribute webDriver
wd.switch_to.frame(frame_reference)
frame_reference frame element may be a name or attribute or a frame ID corresponding WebElement object.
Position can be based on the element characteristic or attribute of the frame, a series find use, select the element, to give the corresponding WebElement object.
Iframe External : Use default_content ()
1 from selenium import webdriver 2 wd = webdriver.Firefox(executable_path=r'D:\BrowserDriver\geckodriver.exe') 3 wd.get('http://cdn1.python3.vip/files/selenium/sample2.html') 4 # 选择内部元素 5 wd.switch_to.frame('innerFrame') 6 # 选择外部元素 7 wd.switch_to.default_content() 8 elements = wd.find_elements_by_class_name('animal') 9 for element in elements: 10 print(element.text) 11 wd.find_element_by_id('outerbutton').click() 12 wd.quit()
2, switch to a new window
1 # switch to a new window: window method webdriver switch_to properties the object. Format: wd.switch_to.window (handle), 2 # webdriver object has window_handles property, which is a list of objects, which included the current browser window handles inside all 3 # Handle: id page window. . 4 '' ' . 5 from the webdriver Import Selenium . 6 WD = webdriver.Firefox (executable_path = r'D: \ BrowserDriver \ geckodriver.exe') . 7 wd.get ( 'http://cdn1.python3.vip/files/selenium/ sample3.html ') . 8 Link wd.find_element_by_tag_name = (' A ') . 9 link.click () 10 for handle in wd.window_handles: . 11 wd.switch_to.window (handle) 12 is IF' Bing 'in wd.title: break 14 print(handle.title)
You can save the current window handle, after the handover the new window, then the driver returns to the original object corresponding to the window:
# MainWindow variable holds the handle of the window
mainWindow = wd.current_window_handle
# Handle previously saved by the old windows, old window switch to their own
wd.switch_to.window (mainWindow)