我们从Python开源项目中,提取了以下28个代码示例,用于说明如何使用selenium.common.exceptions.NoAlertPresentException()。
def AlertAccept(cls): log.step_normal("AlertAccept()") i = 0 while i < 60: try: log.step_normal("switch_to_alert()") alert = env.driver.switch_to_alert() alert.accept() break except NoAlertPresentException: log.step_normal("Alert Not Found. Wait 3 Seconds then Try Again!") time.sleep(3) try: log.step_normal("switch_to_default_content()") env.driver.switch_to_default_content() except: pass
def AlertDismiss(cls): log.step_normal("AlertDismiss()") i = 0 while i < 60: try: log.step_normal("switch_to_alert()") alert = env.driver.switch_to_alert() alert.dismiss() break except NoAlertPresentException: log.step_normal("Alert Not Found. Wait 3 Seconds then Try Again!") time.sleep(3) try: log.step_normal("switch_to_default_content()") env.driver.switch_to_default_content() except: pass
def AlertAccept(cls):#??Alert??? ?? log.step_normal("AlertAccept()") time.sleep(2) try: log.step_normal("switch_to_alert()") alert = env.threadlocal.BROWSER.switch_to_alert() alert.accept() except NoAlertPresentException: log.step_normal("Alert Not Found. ") try: log.step_normal("switch_to_default_content()") env.threadlocal.BROWSER.switch_to_default_content() except: pass
def AlertDismiss(cls): #??Alert??? ?? log.step_normal("AlertDismiss()") time.sleep(2) try: log.step_normal("switch_to_alert()") alert = env.threadlocal.BROWSER.switch_to_alert() alert.dismiss() except NoAlertPresentException: log.step_normal("Alert Not Found.") try: log.step_normal("switch_to_default_content()") env.threadlocal.BROWSER.switch_to_default_content() except: pass
def assert_alert_present(self): ''' Asserts that an alert exists. ''' alert = self.browser.switch_to_alert() msg = 'An alert was not present but was expected.' try: atext = bool(alert.text) except NoAlertPresentException: atext = False assert atext == True, msg
def assert_alert_not_present(self): ''' Asserts that an alert does not exist. ''' def check_text(alert): bool(alert.text) alert = self.browser.switch_to_alert() present = False try: present = bool(alert.text) except NoAlertPresentException: pass assert present == False
def __call__(self, driver): try: alert = driver.switch_to.alert alert.text return alert except NoAlertPresentException: return False
def is_alert_present(self): try: self.driver.switch_to_alert() except NoAlertPresentException as e: return False return True
def assert_exists(self): try: self.alert = self.browser.driver.switch_to.alert self.alert.text except NoAlertPresentException: raise UnknownObjectException('unable to locate alert')
def click_buttons(self): """ consider the consequence of clicking buttons: 1. redirect: well, redirecting usually occurs with tag 'a' rather than 'button' or 'input', so I ignore this situation right now. Maybe I shouldn't? 2. alert: handler alert well :return: """ btns = self.find_elements_by_tag_name("button") for btn in btns: try: logger.debug("find a button and click on it") btn.click() self.switch_to.alert.accept() except NoAlertPresentException: pass except: traceback.print_exc() btns = self.find_elements_by_xpath('//input[@type="button"]') # btns = self.find_elements_by_xpath('//input[@onclick]') for btn in btns: try: btn.click() self.switch_to.alert.accept() except NoAlertPresentException: pass except: traceback.print_exc() # it seems like we can declare that the button isn't inside a form # make sure it isn't inside a form # try: # form = btn.find_element_by_xpath('./ancestor::form') # except NoSuchElementException: # try: # btn.click() # except: # traceback.print_exc() # else: # logger.debug("it is inside a form")
def is_alert_present(self): try: self.driver.switch_to_alert() except NoAlertPresentException: return False return True
def Handle_Browser_Alert(step_data): #accepts browser alert sModuleInfo = inspect.stack()[0][3] + " : " + inspect.getmoduleinfo(__file__).name try: choice = str(step_data[0][2]).lower() if choice == 'accept' or choice == 'pass' or choice == 'yes' or choice == 'ok': try: selenium_driver.switch_to_alert().accept() CommonUtil.ExecLog(sModuleInfo, "Browser alert accepted", 1) return "passed" except NoAlertPresentException as e: CommonUtil.ExecLog(sModuleInfo, "Browser alert not found", 2) return "passed" elif choice == 'reject' or choice == 'fail' or choice == 'no' or choice == 'cancel': try: selenium_driver.switch_to_alert().dismiss() CommonUtil.ExecLog(sModuleInfo, "Browser alert rejected", 1) return "passed" except NoAlertPresentException as e: CommonUtil.ExecLog(sModuleInfo, "Browser alert not found", 2) return "passed" else: CommonUtil.ExecLog(sModuleInfo, "Wrong Step Data", 3) return "failed" except Exception: ErrorMessage = "Failed to accept browser alert" return CommonUtil.Exception_Handler(sys.exc_info(), None, ErrorMessage)