#Encoding=utf-8ImportUnitTestImport Time fromSeleniumImportWebdriver fromSelenium.webdriverImportActionchains fromSelenium.common.exceptionsImporttimeoutexception fromSelenium.webdriver.common.keysImportKeysclassSetpageloadtime (unittest. TestCase):defsetUp (self):#Start Firefox browserSelf.driver = Webdriver. Ie (Executable_path ="E:\\iedriverserver") defTest_pageloadtime (self):#set page load limit time to 4 secondsSelf.driver.set_page_load_timeout (4) Self.driver.maximize_window ()Try: StartTime=time.time () self.driver.get ("http://mail.126.com") excepttimeoutexception:PrintU'page load exceeds set time, timeout' #when the page load time exceeds the set time, #stop loading by executing JavaScript, then continue with subsequent actionsSelf.driver.execute_script ('window.stop ()') End= Time.time ()-StartTimePrintEnd#switch into the frame controlSelf.driver.switch_to.frame ("X-urs-iframe") #Get user name input boxUserName = Self.driver.find_element_by_xpath ('//input[@name = "Email"]') #Enter user nameUsername.send_keys ("XXX") #Get password entry boxPWD = Self.driver.find_element_by_xpath ("//input[@name = ' password ')") #Enter PasswordPwd.send_keys ("XXX") #send a Enter keyPwd.send_keys (Keys.return) time.sleep (5) assertU"Exit" inchSelf.driver.page_sourcedefTearDown (self): Self.driver.quit ()if __name__=='__main__': Unittest.main ()
Selenium webdriver-Specify page load time