1. Multi-form switching
For test page: Embedded Baidu homepage
Switch to Baidu homepage for operation
1 #coding = Utf-82 3 fromSeleniumImportWebdriver4 ImportOS5 Import Time6 7Dr =Webdriver. Firefox ()8File_path ='file:///'+ Os.path.abspath ('frame.html')9 dr.get (File_path)Ten One #Switch to iframe (id= "if") ADr.switch_to.frame ("if") - -DR.FIND_ELEMENT_BY_ID ("kw"). Send_keys ("Selenium") theDR.FIND_ELEMENT_BY_ID ("kw"). Submit () -Time.sleep (3) - -Dr.close ()
Switch_to.frame () can take the ID of the form or the Name property directly. If the IFRAME does not have an ID and a name attribute available. Can be positioned in the following ways
...... # first through XPath to the iframeXF = driver.find_element_by_xpath ('//*[@class = "If"] # then pass the anchored object to the Switch_to.frame () method driver.switch_to.frame (XF) ... driver.switch_to.parent _frame ()
There are other ways switch_to.parent_content (), Switch_to.default_content ()
I didn't use any of these methods.
Python+selenium Learning Note 8-Multiple Forms & Multi-window switching