我们从Python开源项目中,提取了以下15个代码示例,用于说明如何使用selenium.common.exceptions.NoSuchWindowException()。
def state(self): """ Check the current status of the alarm system. """ # Click the refresh button to verify the state if it was made somewhere else try: # Recheck the current status self._driver.get(self._driver.current_url) current_status = WebDriverWait(self._driver, self.timeout).until(EC.presence_of_element_located((self.STATUS_IMG[0], self.STATUS_IMG[1]))).text _LOGGER.debug('Fetched current status from system: {}'.format(current_status)) return current_status except (exceptions.NoSuchElementException, exceptions.NoSuchWindowException, exceptions.TimeoutException, urllib.error.URLError) as e: _LOGGER.warning('Error while checking alarm status. Attempting login again.') self._login() current_status = WebDriverWait(self._driver, self.timeout).until(EC.presence_of_element_located((self.STATUS_IMG[0], self.STATUS_IMG[1]))).text return current_status
def _select_by_default(self, browser, criteria): if criteria is None or len(criteria) == 0 or criteria.lower() == "null": handles = browser.get_window_handles() browser.switch_to_window(handles[0]) return try: starting_handle = browser.get_current_window_handle() except NoSuchWindowException: starting_handle = None for handle in browser.get_window_handles(): browser.switch_to_window(handle) if criteria == handle: return for item in browser.get_current_window_info()[2:4]: if item.strip().lower() == criteria.lower(): return if starting_handle: browser.switch_to_window(starting_handle) raise ValueError("Unable to locate window with handle or name or title or URL '" + criteria + "'")
def switch_window(self, handle=None): """ Switches window. If there are only 2 windows, it can work out which window to switch to. Otherwise, you should pass in the window handle as `handle` kwarg. Returns a tuple (old_window_handle, new_window_handle) """ try: current_window_handle = self._driver.current_window_handle except NoSuchWindowException: # Window closed recently current_window_handle = None window_handles = self._driver.window_handles if handle is None: possible_window_handles = [h for h in window_handles if h != current_window_handle] if len(possible_window_handles) > 1: raise AssertionError("Don't know which window to switch to!") else: handle = possible_window_handles[0] def f(driver): if (hasattr(driver, 'switch_to') and hasattr(driver.switch_to, 'window')): m = driver.switch_to.window else: m = driver.switch_to_window m(handle) return driver.current_window_handle == handle self.wait_until(f) return current_window_handle, handle
def _wait_until_finished(self): try: self.wait_for_page_load() except NoSuchWindowException: pass # window can legitimately close e.g. for popups
def _matches(self, handle): from .locators.element.validator import Validator try: orig = self.driver.current_window_handle except NoSuchWindowException: orig = None try: self.driver.switch_to.window(handle) if 'title' in self.selector: title_value = self.selector.get('title') driver_title = self.driver.title matches_title = Validator.match_str_or_regex(title_value, driver_title) else: matches_title = True if 'url' in self.selector: url_value = self.selector.get('url') driver_url = self.driver.current_url matches_url = Validator.match_str_or_regex(url_value, driver_url) else: matches_url = True return matches_title and matches_url except (NoSuchWindowException, WebDriverException): return False finally: current = self.driver.window_handles orig = orig if orig in current else current[0] self.driver.switch_to.window(orig)
def select_window(self, locator=None): """Selects the window matching locator and return previous window handle. locator: any of name, title, url, window handle, excluded handle's list, or special words. return: either current window handle before selecting, or None if no current window. If the window is found, all subsequent commands use that window, until this keyword is used again. If the window is not found, this keyword fails. By default, when a locator value is provided, it is matched against the title of the window and the javascript name of the window. If multiple windows with same identifier are found, the first one is selected. There are some special locators for searching target window: string 'main' (default): select the main window; string 'self': only return current window handle; string 'new': select the last-indexed window assuming it is the newest opened window window list: select the first window not in given list (See 'List Windows' to get the list) It is also possible to specify the approach Selenium2Library should take to find a window by specifying a locator strategy: | *Strategy* | *Example* | *Description* | | title | Select Window `|` title=My Document | Matches by window title | | name | Select Window `|` name=${name} | Matches by window javascript name | | url | Select Window `|` url=http://google.com | Matches by window's current URL | Example: | Click Link | popup_link | # opens new window | | Select Window | popupName | | Title Should Be | Popup Title | | Select Window | | | # Chooses the main window again | """ try: return self._current_browser().get_current_window_handle() except NoSuchWindowException: pass finally: self._window_manager.select(self._current_browser(), locator)
def _select_by_last_index(self, browser): handles = browser.get_window_handles() try: if handles[-1] == browser.get_current_window_handle(): raise AssertionError("No new window at last index. Please use '@{ex}= | List Windows' + new window trigger + 'Select Window | ${ex}' to find it.") except IndexError: raise AssertionError("No window found") except NoSuchWindowException: raise AssertionError("Currently no focus window. where are you making a popup window?") browser.switch_to_window(handles[-1])
def _get_window_infos(self, browser): window_infos = [] try: starting_handle = browser.get_current_window_handle() except NoSuchWindowException: starting_handle = None try: for handle in browser.get_window_handles(): browser.switch_to_window(handle) window_infos.append(browser.get_current_window_info()) finally: if starting_handle: browser.switch_to_window(starting_handle) return window_infos
def _select_matching(self, browser, matcher, error): try: starting_handle = browser.get_current_window_handle() except NoSuchWindowException: starting_handle = None for handle in browser.get_window_handles(): browser.switch_to_window(handle) if matcher(browser.get_current_window_info()): return if starting_handle: browser.switch_to_window(starting_handle) raise ValueError(error)
def tearDown(self): time.sleep(3) while self.close_manually: try: self.wait_page_loaded() except NoSuchWindowException: self.close_manually = False super(PostgresAdminTestCase, self).tearDown()
def click(self, css_selector=None, xpath=None, text=None, text_parent_id=None, wait_for_reload=False, wait_timeout=None, double=False, scroll=True, window_closes=False): """ Clicks the button or control specified by the CSS selector or xpath """ if window_closes: wait_for_reload = False if wait_for_reload: self._driver.execute_script("document.pageReloadedYetFlag='notyet';") elem = self._find_with_timeout(css_selector=css_selector, xpath=xpath, text=text, text_parent_id=text_parent_id, timeout=wait_timeout) if scroll: self._scroll_into_view(elem) elem.click() if double: try: elem.click() except StaleElementReferenceException: pass if wait_for_reload: def f(driver): obj = driver.execute_script("return document.pageReloadedYetFlag;") if obj is None or obj != "notyet": return True return False try: WebDriverWait(self._driver, self.get_default_timeout()).until(f) except NoSuchWindowException: # legitimate sometimes e.g. when window closes pass if not window_closes: self._wait_until_finished()