#Encoding=utf-8ImportUnitTestImport Time fromSeleniumImportWebdriver fromSelenium.webdriverImportActionchainsclassVisitsogoubyie (unittest. TestCase):defsetUp (self):#Start IE browser #self.driver = Webdriver. Firefox (Executable_path = "E:\\geckodriver")Self.driver = Webdriver. Ie (Executable_path ="E:\\iedriverserver") deftesthandleprompt (self): URL="http://127.0.0.1/test_prompt.html" #Accessing a custom Web pageself.driver.get (URL)#Locate the unique button element on the page being tested using the ID Locator methodelement = self.driver.find_element_by_id ("Button") Element.click () Time.sleep (2) #click the button element to bring up a prompt prompt box, #The above will show "This is a prompt popup", the input box, #OK button and Cancel button #Use the Driver.switch_to_alert () method to get the Alert objectAlert =Self.driver.switch_to.alert#use the Alert.text method to get the text above the prompt box, #and assert that the text content is consistent with "This is a prompt popup"Self.assertequal (U"This is a prompt pop-up box", Alert.text) time.sleep (1) #Call the Alert.send_keys () method and enter it in the input box of the prompt form #"The Road to glory: To change your destiny, you must study 2 hours a day!" "Alert.send_keys (U"The Road to glory: To change your destiny, you must study 2 hours a day! ") Time.sleep (1) #using the Accept method of the Alert object, #Click the "OK" button in the prompt box to close the prompt boxalert.accept ()#using the dismiss method of the Alert object, click the Cancel button on the prompt box to close the prompt box #canceling the comment on the following line of code simulates clicking the Cancel button on the prompt box #Alert.dismiss () defTearDown (self):#Exit IE Browserself.driver.quit () unittest.main ( )
Selenium webdriver-operation JavaScript prompt pop-up window (low utilization)