mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
向浏览器添加cookie时会自动重试保证添加成功,待测试
This commit is contained in:
parent
8a46f7c7f6
commit
ea02a25c5d
@ -42,7 +42,7 @@ class ChromiumBase(BasePage):
|
||||
self._set = None
|
||||
self._screencast = None
|
||||
|
||||
if address and (isinstance(address, int) or address.isdigit()):
|
||||
if isinstance(address, int) or (isinstance(address, str) and address.isdigit()):
|
||||
address = f'127.0.0.1:{address}'
|
||||
|
||||
self._set_start_options(address, None)
|
||||
|
@ -109,7 +109,10 @@ class ChromiumPage(ChromiumBase):
|
||||
pass
|
||||
|
||||
self._process_id = None
|
||||
for i in self.browser_driver.SystemInfo.getProcessInfo()['processInfo']:
|
||||
r = self.browser_driver.SystemInfo.getProcessInfo()
|
||||
if 'processInfo' not in r:
|
||||
return None
|
||||
for i in r['processInfo']:
|
||||
if i['type'] == 'browser':
|
||||
self._process_id = i['id']
|
||||
break
|
||||
|
@ -253,15 +253,41 @@ def set_browser_cookies(page, cookies):
|
||||
cookie.pop('expiry')
|
||||
if 'expires' in cookie:
|
||||
cookie['expires'] = int(cookie['expires'])
|
||||
|
||||
if not cookie.get('domain', None):
|
||||
ex_url = extract(page._browser_url)
|
||||
cookie['domain'] = f'{ex_url.domain}.{ex_url.suffix}' if ex_url.suffix else ex_url.domain
|
||||
|
||||
if cookie['value'] is None:
|
||||
cookie['value'] = ''
|
||||
|
||||
try:
|
||||
if cookie.get('domain', None):
|
||||
try:
|
||||
page.run_cdp_loaded('Network.setCookie', **cookie)
|
||||
continue
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
ex_url = extract(page._browser_url)
|
||||
d_list = ex_url.subdomain.split('.')
|
||||
d_list.append(f'{ex_url.domain}.{ex_url.suffix}' if ex_url.suffix else ex_url.domain)
|
||||
|
||||
for i in range(len(d_list)):
|
||||
d = f'.{".".join(d_list[i:])}'
|
||||
cookie['domain'] = d
|
||||
page.run_cdp_loaded('Network.setCookie', **cookie)
|
||||
except Exception:
|
||||
pass
|
||||
if is_cookie_in_driver(page, cookie):
|
||||
continue
|
||||
|
||||
d = f'{".".join(d_list[i:])}'
|
||||
cookie['domain'] = d
|
||||
page.run_cdp_loaded('Network.setCookie', **cookie)
|
||||
if is_cookie_in_driver(page, cookie):
|
||||
continue
|
||||
|
||||
|
||||
def is_cookie_in_driver(page, cookie):
|
||||
"""查询cookie是否在浏览器内
|
||||
:param page: BasePage对象
|
||||
:param cookie: dict格式cookie
|
||||
:return: bool
|
||||
"""
|
||||
for c in page.get_cookies():
|
||||
if cookie['name'] == c['name'] and cookie['value'] == c['value']:
|
||||
return True
|
||||
return False
|
||||
|
@ -42,3 +42,6 @@ def set_session_cookies(session: Session, cookies: Union[RequestsCookieJar, list
|
||||
|
||||
|
||||
def set_browser_cookies(page: ChromiumBase, cookies: Union[RequestsCookieJar, list, tuple, str, dict]) -> None: ...
|
||||
|
||||
|
||||
def is_cookie_in_driver(page: ChromiumBase, cookie: dict) -> bool: ...
|
||||
|
Loading…
x
Reference in New Issue
Block a user