本文整理汇总了Python中selenium.common.exceptions.NoSuchWindowException方法的典型用法代码示例。如果您正苦于以下问题:Python exceptions.NoSuchWindowException方法的具体用法?Python exceptions.NoSuchWindowException怎么用?Python exceptions.NoSuchWindowException使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类selenium.common.exceptions
的用法示例。
在下文中一共展示了exceptions.NoSuchWindowException方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: find_window_handle
# 需要导入模块: from selenium.common import exceptions [as 别名]
# 或者: from selenium.common.exceptions import NoSuchWindowException [as 别名]
def find_window_handle(webdriver, predicate):
original_window_handle = webdriver.current_window_handle
if predicate(webdriver):
return original_window_handle
# Start search beginning with the most recently added window handle: the chance is higher that this is the correct
# one in most cases
for window_handle in reversed(webdriver.window_handles):
if window_handle == original_window_handle:
continue
# This exception can occur if the window handle was closed between accessing the window handles and attempting
# to switch to it, in which case it can be silently ignored.
try:
webdriver.switch_to.window(window_handle)
except NoSuchWindowException:
continue
if predicate(webdriver):
return window_handle
# Simply switch back to the original window handle and return None if no matching window handle was found
webdriver.switch_to.window(original_window_handle)
示例2: openChrome
# 需要导入模块: from selenium.common import exceptions [as 别名]
# 或者: from selenium.common.exceptions import NoSuchWindowException [as 别名]
def openChrome(paydetailsO, itemdetsO, timeO, strictO, skipO, nextO, cdloc, capabilities, useProxy, PROXY):
global driver, strict, password, reg, items, droptime, pDescr, paydetails, category, skipS, nextS
chrome_options = webdriver.ChromeOptions()
if useProxy:
prx = Proxy()
prx.proxy_type = ProxyType.MANUAL
prx.http_proxy = PROXY
prx.socks_proxy = PROXY
prx.ssl_proxy = PROXY
prx.add_to_capabilities(capabilities)
else:
prx = Proxy()
prx.proxy_type = ProxyType.SYSTEM
prx.add_to_capabilities(capabilities)
chrome_options.binary_location = capabilities['chrome.binary']
driver = webdriver.Chrome(cdloc, desired_capabilities=capabilities)
openTab('https://www.google.com', driver)
paydetails = paydetailsO
reg = paydetailsO['Region']
strict = strictO
skipS = skipO
nextS = nextO
droptime = timeO
items = []
for x in itemdetsO:
print(x[0],x[1],x[2],x[3])
items.append({'selectedCategory': x[0], 'keywords': x[1].split(','), 'selectedColour': x[2], 'selectedSize': x[3]})
returnTime()
try:
for it in items:
searchItem(it)
cart()
except NoSuchWindowException:
print('[!] Chrome window closed. Click GO! to re-start')
return None
示例3: close_all_windows_except_first
# 需要导入模块: from selenium.common import exceptions [as 别名]
# 或者: from selenium.common.exceptions import NoSuchWindowException [as 别名]
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])
示例4: switch_window
# 需要导入模块: from selenium.common import exceptions [as 别名]
# 或者: from selenium.common.exceptions import NoSuchWindowException [as 别名]
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
示例5: _wait_until_finished
# 需要导入模块: from selenium.common import exceptions [as 别名]
# 或者: from selenium.common.exceptions import NoSuchWindowException [as 别名]
def _wait_until_finished(self):
try:
self.wait_for_page_load()
except NoSuchWindowException:
pass # window can legitimately close e.g. for popups
示例6: current_tab_id
# 需要导入模块: from selenium.common import exceptions [as 别名]
# 或者: from selenium.common.exceptions import NoSuchWindowException [as 别名]
def current_tab_id(self):
try:
return self.driver.current_window_handle
except selenium_exceptions.NoSuchWindowException:
print('Failed to get current tab_id, likely close. Switch to tab 0')
try:
self.to_tab_by_index(0)
return self.driver.current_window_handle
except:
print('Still failed to get current tab_id')
raise
except:
print('Failed to get current tab_id for unexpected reason')
raise
示例7: make_match_domain_predicate
# 需要导入模块: from selenium.common import exceptions [as 别名]
# 或者: from selenium.common.exceptions import NoSuchWindowException [as 别名]
def make_match_domain_predicate(domain):
def predicate(webdriver):
try:
return get_tld(webdriver.current_url) == domain
# This exception can occur if the current window handle was closed
except NoSuchWindowException:
pass
return predicate
示例8: _matches
# 需要导入模块: from selenium.common import exceptions [as 别名]
# 或者: from selenium.common.exceptions import NoSuchWindowException [as 别名]
def _matches(self, handle):
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.browser.title
matches_title = re.search(title_value, driver_title) is not None
else:
matches_title = True
if 'url' in self.selector:
url_value = self.selector.get('url')
driver_url = self.browser.url
matches_url = re.search(url_value, driver_url) is not None
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)
示例9: run
# 需要导入模块: from selenium.common import exceptions [as 别名]
# 或者: from selenium.common.exceptions import NoSuchWindowException [as 别名]
def run(self):
""" Runs after hooks """
# We can't just rescue exception because Firefox automatically closes alert when
# exception raised
try:
if self.after_hooks and not self.browser.alert.exists:
for hook in self.after_hooks:
hook(self.browser)
except NoSuchWindowException as e:
nerodia.logger.info('Could not execute After Hooks because browser window was '
'closed {}'.format(e))
示例10: click
# 需要导入模块: from selenium.common import exceptions [as 别名]
# 或者: from selenium.common.exceptions import NoSuchWindowException [as 别名]
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()