mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
4.1.0.0b3修复清空和删除cookies问题
This commit is contained in:
parent
2ea2be782f
commit
58285fc0ad
@ -14,4 +14,4 @@ from ._pages.chromium_page import ChromiumPage
|
||||
from ._pages.mix_page import MixPage
|
||||
from ._pages.mix_page import MixPage as WebPage
|
||||
|
||||
__version__ = '4.1.0.0b2'
|
||||
__version__ = '4.1.0.0b3'
|
||||
|
@ -8,13 +8,27 @@
|
||||
from .._functions.cookies import set_tab_cookies, set_session_cookies, set_browser_cookies
|
||||
|
||||
|
||||
class CookiesSetter(object):
|
||||
class BrowserCookiesSetter(object):
|
||||
def __init__(self, owner):
|
||||
"""
|
||||
:param owner: ChromiumBase对象
|
||||
:param owner: Chromium对象
|
||||
"""
|
||||
self._owner = owner
|
||||
|
||||
def __call__(self, cookies):
|
||||
"""设置一个或多个cookie
|
||||
:param cookies: cookies信息
|
||||
:return: None
|
||||
"""
|
||||
set_browser_cookies(self._owner, cookies)
|
||||
|
||||
def clear(self):
|
||||
"""清除cookies"""
|
||||
self._owner._run_cdp('Storage.clearCookies')
|
||||
|
||||
|
||||
class CookiesSetter(BrowserCookiesSetter):
|
||||
|
||||
def __call__(self, cookies):
|
||||
"""设置一个或多个cookie
|
||||
:param cookies: cookies信息
|
||||
@ -22,6 +36,10 @@ class CookiesSetter(object):
|
||||
"""
|
||||
set_tab_cookies(self._owner, cookies)
|
||||
|
||||
def clear(self):
|
||||
"""清除cookies"""
|
||||
self._owner._run_cdp('Network.clearBrowserCookies')
|
||||
|
||||
def remove(self, name, url=None, domain=None, path=None):
|
||||
"""删除一个cookie
|
||||
:param name: cookie的name字段
|
||||
@ -37,23 +55,11 @@ class CookiesSetter(object):
|
||||
d['domain'] = domain
|
||||
if not url and not domain:
|
||||
d['url'] = self._owner.url
|
||||
if not d['url'].startswith('http'):
|
||||
raise ValueError('需设置domain或url值。如设置url值,需以http开头。')
|
||||
if path is not None:
|
||||
d['path'] = path
|
||||
self._owner._run_cdp('Storage.deleteCookies', **d)
|
||||
|
||||
def clear(self):
|
||||
"""清除cookies"""
|
||||
self._owner._run_cdp('Storage.clearBrowserCookies')
|
||||
|
||||
|
||||
class BrowserCookiesSetter(CookiesSetter):
|
||||
|
||||
def __call__(self, cookies):
|
||||
"""设置一个或多个cookie
|
||||
:param cookies: cookies信息
|
||||
:return: None
|
||||
"""
|
||||
set_browser_cookies(self._owner, cookies)
|
||||
self._owner._run_cdp('Network.deleteCookies', **d)
|
||||
|
||||
|
||||
class SessionCookiesSetter(object):
|
||||
|
@ -15,22 +15,24 @@ from .._pages.session_page import SessionPage
|
||||
from .._pages.mix_page import MixPage
|
||||
|
||||
|
||||
class CookiesSetter(object):
|
||||
_owner: ChromiumBase
|
||||
class BrowserCookiesSetter(object):
|
||||
_owner: Chromium = ...
|
||||
|
||||
def __init__(self, page: ChromiumBase): ...
|
||||
def __init__(self, page: Chromium): ...
|
||||
|
||||
def __call__(self, cookies: Union[CookieJar, Cookie, list, tuple, str, dict]) -> None: ...
|
||||
|
||||
def remove(self, name: str, url: str = None, domain: str = None, path: str = None) -> None: ...
|
||||
|
||||
def clear(self) -> None: ...
|
||||
|
||||
|
||||
class BrowserCookiesSetter(CookiesSetter):
|
||||
_owner: Chromium = ...
|
||||
class CookiesSetter(BrowserCookiesSetter):
|
||||
_owner: ChromiumBase = ...
|
||||
|
||||
def __init__(self, page: Chromium): ...
|
||||
def __init__(self, page: ChromiumBase): ...
|
||||
|
||||
def remove(self, name: str, url: str = None, domain: str = None, path: str = None) -> None: ...
|
||||
|
||||
def clear(self) -> None: ...
|
||||
|
||||
|
||||
class SessionCookiesSetter(object):
|
||||
|
@ -30,6 +30,27 @@ class BrowserWaiter(OriginWaiter):
|
||||
def __init__(self, owner):
|
||||
self._owner = owner
|
||||
|
||||
def new_tab(self, timeout=None, curr_tab=None, raise_err=None):
|
||||
"""等待新标签页出现
|
||||
:param timeout: 超时时间(秒),为None则使用页面对象timeout属性
|
||||
:param curr_tab: 指定当前最新的tab id,用于判断新tab出现,为None自动获取
|
||||
:param raise_err: 等待失败时是否报错,为None时根据Settings设置
|
||||
:return: 等到新标签页返回其id,否则返回False
|
||||
"""
|
||||
curr_tid = curr_tab if curr_tab else self._owner.tab_ids[0]
|
||||
timeout = timeout if timeout is not None else self._owner.timeout
|
||||
end_time = perf_counter() + timeout
|
||||
while perf_counter() < end_time:
|
||||
latest_tid = self._owner.tab_ids[0]
|
||||
if curr_tid != latest_tid:
|
||||
return latest_tid
|
||||
sleep(.01)
|
||||
|
||||
if raise_err is True or Settings.raise_when_wait_failed is True:
|
||||
raise WaitTimeoutError(f'等待新标签页失败(等待{timeout}秒)。')
|
||||
else:
|
||||
return False
|
||||
|
||||
def download_begin(self, timeout=None, cancel_it=False):
|
||||
"""等待浏览器下载开始,可将其拦截
|
||||
:param timeout: 超时时间(秒),None使用页面对象超时时间
|
||||
@ -54,27 +75,6 @@ class BrowserWaiter(OriginWaiter):
|
||||
self._owner._dl_mgr.set_flag('browser', None)
|
||||
return r
|
||||
|
||||
def new_tab(self, timeout=None, curr_tab=None, raise_err=None):
|
||||
"""等待新标签页出现
|
||||
:param timeout: 超时时间(秒),为None则使用页面对象timeout属性
|
||||
:param curr_tab: 指定当前最新的tab id,用于判断新tab出现,为None自动获取
|
||||
:param raise_err: 等待失败时是否报错,为None时根据Settings设置
|
||||
:return: 等到新标签页返回其id,否则返回False
|
||||
"""
|
||||
curr_tid = curr_tab if curr_tab else self._owner.tab_ids[0]
|
||||
timeout = timeout if timeout is not None else self._owner.timeout
|
||||
end_time = perf_counter() + timeout
|
||||
while perf_counter() < end_time:
|
||||
latest_tid = self._owner.tab_ids[0]
|
||||
if curr_tid != latest_tid:
|
||||
return latest_tid
|
||||
sleep(.01)
|
||||
|
||||
if raise_err is True or Settings.raise_when_wait_failed is True:
|
||||
raise WaitTimeoutError(f'等待新标签页失败(等待{timeout}秒)。')
|
||||
else:
|
||||
return False
|
||||
|
||||
def all_downloads_done(self, timeout=None, cancel_if_timeout=True):
|
||||
"""等待所有浏览器下载任务结束
|
||||
:param timeout: 超时时间(秒),为None时无限等待
|
||||
@ -332,6 +332,7 @@ class BaseWaiter(OriginWaiter):
|
||||
|
||||
|
||||
class TabWaiter(BaseWaiter):
|
||||
"""标签页对象等待对象"""
|
||||
|
||||
def downloads_done(self, timeout=None, cancel_if_timeout=True):
|
||||
"""等待所有浏览器下载任务结束
|
||||
@ -370,6 +371,8 @@ class TabWaiter(BaseWaiter):
|
||||
|
||||
|
||||
class PageWaiter(TabWaiter):
|
||||
"""ChromiumPage和MixPage的等待对象"""
|
||||
|
||||
def __init__(self, page):
|
||||
super().__init__(page)
|
||||
|
||||
@ -379,18 +382,7 @@ class PageWaiter(TabWaiter):
|
||||
:param raise_err: 等待失败时是否报错,为None时根据Settings设置
|
||||
:return: 等到新标签页返回其id,否则返回False
|
||||
"""
|
||||
timeout = timeout if timeout is not None else self._driver.timeout
|
||||
end_time = perf_counter() + timeout
|
||||
while perf_counter() < end_time:
|
||||
latest_tid = self._driver.tab_ids[0]
|
||||
if self._driver.tab_id != latest_tid:
|
||||
return latest_tid
|
||||
sleep(.01)
|
||||
|
||||
if raise_err is True or Settings.raise_when_wait_failed is True:
|
||||
raise WaitTimeoutError(f'等待新标签页失败(等待{timeout}秒)。')
|
||||
else:
|
||||
return False
|
||||
return self._driver.browser.wait.new_tab(timeout=timeout, raise_err=raise_err)
|
||||
|
||||
def all_downloads_done(self, timeout=None, cancel_if_timeout=True):
|
||||
"""等待所有浏览器下载任务结束
|
||||
@ -398,27 +390,7 @@ class PageWaiter(TabWaiter):
|
||||
:param cancel_if_timeout: 超时时是否取消剩余任务
|
||||
:return: 是否等待成功
|
||||
"""
|
||||
if not self._driver.browser._dl_mgr._running:
|
||||
raise RuntimeError('此功能需显式设置下载路径(使用set.download_path()方法、配置对象或ini文件均可)。')
|
||||
if not timeout:
|
||||
while self._driver.browser._dl_mgr._missions:
|
||||
sleep(.5)
|
||||
return True
|
||||
|
||||
else:
|
||||
end_time = perf_counter() + timeout
|
||||
while perf_counter() < end_time:
|
||||
if not self._driver.browser._dl_mgr._missions:
|
||||
return True
|
||||
sleep(.5)
|
||||
|
||||
if self._driver.browser._dl_mgr._missions:
|
||||
if cancel_if_timeout:
|
||||
for m in list(self._driver.browser._dl_mgr._missions.values()):
|
||||
m.cancel()
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
return self._driver.browser.wait.all_downloads_done(timeout=timeout, cancel_if_timeout=cancel_if_timeout)
|
||||
|
||||
|
||||
class ElementWaiter(OriginWaiter):
|
||||
@ -427,7 +399,7 @@ class ElementWaiter(OriginWaiter):
|
||||
def __init__(self, owner, ele):
|
||||
"""等待元素在dom中某种状态,如删除、显示、隐藏
|
||||
:param owner: 元素所在页面
|
||||
:param ele: 要等待的元素
|
||||
:param ele: 要执行等待的元素
|
||||
"""
|
||||
self._owner = owner
|
||||
self._ele = ele
|
||||
|
Loading…
x
Reference in New Issue
Block a user