Python源码示例:selenium.common.exceptions.NoAlertPresentException()
示例1
def AlertAccept(self):
logger.step_normal("AlertAccept()")
time.sleep(2)
try:
logger.step_normal("switch_to_alert()")
alert = Browser.RunningBrowser.switch_to_alert()
alert.accept()
except NoAlertPresentException:
logger.step_normal("Alert Not Found. ")
try:
logger.step_normal("switch_to_default_content()")
Browser.RunningBrowser.switch_to_default_content()
except Exception as e:
logger.step_warning(e)
pass
示例2
def AlertDismiss(self):
logger.step_normal("AlertDismiss()")
time.sleep(2)
try:
logger.step_normal("switch_to_alert()")
alert = Browser.RunningBrowser.switch_to_alert()
alert.dismiss()
except NoAlertPresentException:
logger.step_normal("Alert Not Found.")
try:
logger.step_normal("switch_to_default_content()")
Browser.RunningBrowser.switch_to_default_content()
except Exception as e:
logger.step_normal(e)
pass
示例3
def _get(browser, url, retries=5):
try:
browser.get(url)
assert browser.get_cookies()
except TimeoutException:
if retries == 0:
raise
else:
print("Failed to load '{}', trying again...".format(url))
_get(browser, url, retries=retries - 1)
try:
alert = browser.switch_to.alert
except NoAlertPresentException:
pass
else:
print("Warning: dismissing unexpected alert ({})".format(alert.text))
alert.accept()
示例4
def is_alert_present(self):
try:
self.driver.switch_to_alert()
except NoAlertPresentException as e:
return False
return True
示例5
def alert_is_display(self):
"""
selenium API
Determines if alert is displayed
"""
try:
self.driver.switch_to.alert
except NoAlertPresentException:
return False
else:
return True
示例6
def is_alert_present(self):
try:
self.driver.switch_to_alert()
except NoAlertPresentException as e:
return False
return True
示例7
def close_all_windows_except_first(driver):
windows = driver.window_handles
for window in windows[1:]:
driver.switch_to_window(window)
driver.close()
while True:
try:
alert = driver.switch_to_alert()
alert.dismiss()
except (NoAlertPresentException, NoSuchWindowException):
break
driver.switch_to_window(windows[0])
示例8
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
示例9
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
示例10
def close_all_windows_except_first(driver):
windows = driver.window_handles
for window in windows[1:]:
driver.switch_to_window(window)
driver.close()
while True:
try:
alert = driver.switch_to_alert()
alert.dismiss()
except (NoAlertPresentException, NoSuchWindowException):
break
driver.switch_to_window(windows[0])
示例11
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
示例12
def tearDown(self):
self.driver.get(self.base_url)
try: # pragma: no cover
# Accept any "Are you sure to leave?" alert
self.driver.switch_to.alert.accept()
self.driver.switch_to.default_content()
except NoAlertPresentException:
pass
WebDriverWait(self.driver, 10).until(lambda driver: self.base_url == driver.current_url)
self.driver.delete_all_cookies()
self.clearBBDD()
示例13
def is_alert_present(self):
try:
self.get_alert_text()
return True
except NoAlertPresentException:
return False
示例14
def accept_alert(self, ignore_not_present=False):
"""Accepts alert.
:Args:
- ignore_not_present: ignore NoAlertPresentException
"""
try:
self.switch_to.alert.accept()
except NoAlertPresentException:
if not ignore_not_present:
raise
示例15
def alert_is_present(self):
"""Returns whether an alert is present"""
try:
self.switch_to.alert
return True
except NoAlertPresentException:
return False
示例16
def dismiss_alert(self, ignore_not_present=False):
"""Dismiss alert.
:Args:
- ignore_not_present: ignore NoAlertPresentException
"""
try:
self.switch_to.alert.dismiss()
except NoAlertPresentException:
if not ignore_not_present:
raise
示例17
def assert_exists(self):
try:
self.alert = self.browser.driver.switch_to.alert
except NoAlertPresentException:
raise UnknownObjectException('unable to locate alert')
示例18
def _load_notebook(browser, port, notebook, retries=5):
# go to the correct page
url = "http://localhost:{}/notebooks/{}.ipynb".format(port, notebook)
browser.get(url)
alert = ''
for _ in range(5):
if alert is None:
break
try:
alert = browser.switch_to.alert
except NoAlertPresentException:
alert = None
else:
print("Warning: dismissing unexpected alert ({})".format(alert.text))
alert.accept()
def page_loaded(browser):
return browser.execute_script(
"""
return (typeof Jupyter !== "undefined" &&
Jupyter.page !== undefined &&
Jupyter.notebook !== undefined &&
$("#notebook_name").text() === "{}");
""".format(notebook))
# wait for the page to load
try:
_wait(browser).until(page_loaded)
except TimeoutException:
if retries > 0:
print("Retrying page load...")
# page timeout, but sometimes this happens, so try refreshing?
_load_notebook(browser, port, retries=retries - 1, notebook=notebook)
else:
print("Failed to load the page too many times")
raise
示例19
def _close_browser(browser):
browser.save_screenshot(os.path.join(os.path.dirname(__file__), 'selenium.screenshot.png'))
browser.get("about:blank")
try:
alert = browser.switch_to.alert
except NoAlertPresentException:
pass
else:
print("Warning: dismissing unexpected alert ({})".format(alert.text))
alert.accept()
browser.quit()
示例20
def load_page(driver, url, cookies=0):
try:
if cookies == 0:
driver.delete_all_cookies()
time.sleep(1)
if "http" not in url.split("/")[0]:
url = "http://" + url
switch_tab(driver)
driver.get(url)
logging.debug("driver.get(%s) returned successfully" % url)
except (TimeoutException, TimeoutError) as te:
logging.warning("Loading %s timed out" % url)
return str(te)
# try:
# element = WebDriverWait(driver, .5).until(EC.alert_is_present())
# if element is not None:
# print "Alert found on page: " + url
# sys.stdout.flush()
# raise TimeoutError
# else:
# raise NoAlertPresentException
# except (TimeoutException, NoAlertPresentException):
# print "No alert found on page: " + url
# sys.stdout.flush()
# pass
# except TimeoutError as te:
# sys.stdout.flush()
# return str(te)
# try:
# main_handle = driver.current_window_handle
# except CannotSendRequest as csr:
# return str(csr)
try:
windows = driver.window_handles
if len(windows) > 1:
logging.debug("Pop up detected on page: %s. Closing driver instance." % url)
raise TimeoutError
# for window in windows:
# if window != main_handle:
# driver.switch_to_window(window)
# driver.close()
# driver.switch_to_window(main_handle)
# wfrs_status = wait_for_ready_state(driver, 15, 'complete')
# if wfrs_status == "Timed-out":
# print "wait_for_ready_state() timed out."
# raise TimeoutError
except (TimeoutException, TimeoutError) as te:
logging.warning("Loading %s timed out" % url)
return str(te)
return "No Error"
示例21
def _load_notebook(browser, port, retries=5, name="blank"):
# go to the correct page
url = "http://localhost:{}/notebooks/{}.ipynb".format(port, name)
browser.get(url)
alert = ''
for _ in range(5):
if alert is None:
break
try:
alert = browser.switch_to.alert
except NoAlertPresentException:
alert = None
else:
print("Warning: dismissing unexpected alert ({})".format(alert.text))
alert.accept()
def page_loaded(browser):
return browser.execute_script(
"""
return (typeof Jupyter !== "undefined" &&
Jupyter.page !== undefined &&
Jupyter.notebook !== undefined &&
$("#notebook_name").text() === "{}" &&
Jupyter.notebook._fully_loaded);
""".format(name))
# wait for the page to load
try:
_wait(browser).until(page_loaded)
except TimeoutException:
if retries > 0:
print("Retrying page load...")
# page timeout, but sometimes this happens, so try refreshing?
_load_notebook(browser, port, retries=retries - 1, name=name)
else:
print("Failed to load the page too many times")
raise
# Hack: there seems to be some race condition here where sometimes the
# page is still not fully loaded, but I can't figure out exactly what I need
# to check for to ensure that it is. So for now just add a small sleep to
# make sure everything finishes loading, though this is not really a robust
# fix for the problem :/
time.sleep(1)
# delete all cells
if name == "blank":
cells = browser.find_elements_by_css_selector(".cell")
for _ in range(len(cells)):
element = browser.find_elements_by_css_selector(".cell")[0]
element.click()
element.send_keys(Keys.ESCAPE)
element.send_keys("d")
element.send_keys("d")