mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
3.2.14修复切换模式时cookies问题;改进iframe不稳定等待
This commit is contained in:
parent
ad7f871e8e
commit
c2a29dc066
@ -922,13 +922,14 @@ class ChromiumBaseSetter(object):
|
|||||||
cookies = cookies_to_tuple(cookies)
|
cookies = cookies_to_tuple(cookies)
|
||||||
result_cookies = []
|
result_cookies = []
|
||||||
for cookie in cookies:
|
for cookie in cookies:
|
||||||
if not cookie.get('domain', None):
|
if cookie.get('domain', None) is None:
|
||||||
netloc = urlparse(self._page.url).netloc
|
netloc = urlparse(self._page.url).netloc
|
||||||
if netloc.replace('.', '').isdigit(): # ip
|
if netloc.replace('.', '').isdigit(): # ip
|
||||||
cookie['domain'] = netloc
|
cookie['domain'] = netloc
|
||||||
else: # 域名
|
else: # 域名
|
||||||
u = netloc.split('.')
|
u = netloc.split('.')
|
||||||
cookie['domain'] = f'.{u[-2]}.{u[-1]}' if len(u) > 1 else netloc
|
cookie['domain'] = f'.{u[-2]}.{u[-1]}' if len(u) > 1 else netloc
|
||||||
|
|
||||||
result_cookies.append({'value': '' if cookie['value'] is None else cookie['value'],
|
result_cookies.append({'value': '' if cookie['value'] is None else cookie['value'],
|
||||||
'name': cookie['name'],
|
'name': cookie['name'],
|
||||||
'domain': cookie['domain']})
|
'domain': cookie['domain']})
|
||||||
|
@ -129,9 +129,21 @@ class ChromiumFrame(ChromiumBase):
|
|||||||
self._is_loading = False
|
self._is_loading = False
|
||||||
self._is_reading = False
|
self._is_reading = False
|
||||||
|
|
||||||
|
def _onFrameNavigated(self, **kwargs):
|
||||||
|
"""页面跳转时触发"""
|
||||||
|
if kwargs['frame']['frameId'] == self.frame_id and self._first_run is False and self._is_loading:
|
||||||
|
self._is_loading = True
|
||||||
|
|
||||||
|
if self._debug:
|
||||||
|
print('navigated')
|
||||||
|
if self._debug_recorder:
|
||||||
|
self._debug_recorder.add_data((perf_counter(), '加载流程', 'navigated'))
|
||||||
|
|
||||||
def _onLoadEventFired(self, **kwargs):
|
def _onLoadEventFired(self, **kwargs):
|
||||||
"""在页面刷新、变化后重新读取页面内容"""
|
"""在页面刷新、变化后重新读取页面内容"""
|
||||||
# 用于覆盖父类方法,不能删
|
# 用于覆盖父类方法,不能删
|
||||||
|
self._get_new_document()
|
||||||
|
|
||||||
if self._debug:
|
if self._debug:
|
||||||
print('loadEventFired')
|
print('loadEventFired')
|
||||||
if self._debug_recorder:
|
if self._debug_recorder:
|
||||||
|
@ -108,10 +108,8 @@ class WebPageTab(SessionPage, ChromiumTab):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def cookies(self):
|
def cookies(self):
|
||||||
if self._mode == 's':
|
"""以dict方式返回cookies"""
|
||||||
return super().get_cookies()
|
return super().cookies
|
||||||
elif self._mode == 'd':
|
|
||||||
return super(SessionPage, self).get_cookies()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def session(self):
|
def session(self):
|
||||||
@ -282,7 +280,7 @@ class WebPageTab(SessionPage, ChromiumTab):
|
|||||||
selenium_user_agent = self.run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value']
|
selenium_user_agent = self.run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value']
|
||||||
self.session.headers.update({"User-Agent": selenium_user_agent})
|
self.session.headers.update({"User-Agent": selenium_user_agent})
|
||||||
|
|
||||||
self.set.cookies(self._get_driver_cookies(as_dict=False, all_info=False), set_session=True)
|
self.set.cookies(self._get_driver_cookies(as_dict=True, all_info=False), set_session=True)
|
||||||
|
|
||||||
def cookies_to_browser(self):
|
def cookies_to_browser(self):
|
||||||
"""把session对象的cookies复制到浏览器"""
|
"""把session对象的cookies复制到浏览器"""
|
||||||
@ -294,7 +292,7 @@ class WebPageTab(SessionPage, ChromiumTab):
|
|||||||
domain = f'.{u[-2]}.{u[-1]}' if len(u) > 1 else netloc
|
domain = f'.{u[-2]}.{u[-1]}' if len(u) > 1 else netloc
|
||||||
cookies = []
|
cookies = []
|
||||||
for cookie in super().get_cookies():
|
for cookie in super().get_cookies():
|
||||||
if not cookie.get('domain', None):
|
if cookie.get('domain', None) is None:
|
||||||
cookie['domain'] = domain
|
cookie['domain'] = domain
|
||||||
|
|
||||||
if domain in cookie['domain']:
|
if domain in cookie['domain']:
|
||||||
|
@ -57,7 +57,7 @@ class WebPageTab(SessionPage, ChromiumTab):
|
|||||||
def mode(self) -> str: ...
|
def mode(self) -> str: ...
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def cookies(self) -> Union[dict, list]: ...
|
def cookies(self) -> dict: ...
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def session(self) -> Session: ...
|
def session(self) -> Session: ...
|
||||||
|
@ -266,7 +266,7 @@ class Drission(object):
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
browser_domain = ''
|
browser_domain = ''
|
||||||
|
|
||||||
if not cookie.get('domain', None):
|
if cookie.get('domain', None) is None:
|
||||||
if browser_domain:
|
if browser_domain:
|
||||||
if browser_domain.replace('.', '').isdigit(): # ip
|
if browser_domain.replace('.', '').isdigit(): # ip
|
||||||
cookie_domain = browser_domain
|
cookie_domain = browser_domain
|
||||||
|
@ -177,10 +177,8 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def cookies(self):
|
def cookies(self):
|
||||||
if self._mode == 's':
|
"""以dict方式返回cookies"""
|
||||||
return super().get_cookies()
|
return super().cookies
|
||||||
elif self._mode == 'd':
|
|
||||||
return super(SessionPage, self).get_cookies()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def session(self):
|
def session(self):
|
||||||
@ -356,7 +354,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
|
|||||||
selenium_user_agent = self.run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value']
|
selenium_user_agent = self.run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value']
|
||||||
self.session.headers.update({"User-Agent": selenium_user_agent})
|
self.session.headers.update({"User-Agent": selenium_user_agent})
|
||||||
|
|
||||||
self.set.cookies(self._get_driver_cookies(as_dict=False, all_info=False), set_session=True)
|
self.set.cookies(self._get_driver_cookies(as_dict=True), set_session=True)
|
||||||
|
|
||||||
def cookies_to_browser(self):
|
def cookies_to_browser(self):
|
||||||
"""把session对象的cookies复制到浏览器"""
|
"""把session对象的cookies复制到浏览器"""
|
||||||
@ -368,7 +366,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
|
|||||||
domain = f'.{u[-2]}.{u[-1]}' if len(u) > 1 else netloc
|
domain = f'.{u[-2]}.{u[-1]}' if len(u) > 1 else netloc
|
||||||
cookies = []
|
cookies = []
|
||||||
for cookie in super().get_cookies():
|
for cookie in super().get_cookies():
|
||||||
if not cookie.get('domain', None):
|
if cookie.get('domain', None) is None:
|
||||||
cookie['domain'] = domain
|
cookie['domain'] = domain
|
||||||
|
|
||||||
if domain in cookie['domain']:
|
if domain in cookie['domain']:
|
||||||
@ -460,7 +458,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
|
|||||||
self._has_driver = None
|
self._has_driver = None
|
||||||
|
|
||||||
# ---------------准备废弃---------------
|
# ---------------准备废弃---------------
|
||||||
def set_cookies(self, cookies, set_session=False, set_driver=False):
|
def set_cookies(self, cookies, set_session=True, set_driver=True):
|
||||||
"""添加cookies信息到浏览器或session对象
|
"""添加cookies信息到浏览器或session对象
|
||||||
:param cookies: 可以接收`CookieJar`、`list`、`tuple`、`str`、`dict`格式的`cookies`
|
:param cookies: 可以接收`CookieJar`、`list`、`tuple`、`str`、`dict`格式的`cookies`
|
||||||
:param set_session: 是否设置到Session对象
|
:param set_session: 是否设置到Session对象
|
||||||
|
@ -62,7 +62,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
|
|||||||
def mode(self) -> str: ...
|
def mode(self) -> str: ...
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def cookies(self) -> Union[dict, list]: ...
|
def cookies(self) -> dict: ...
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def session(self) -> Session: ...
|
def session(self) -> Session: ...
|
||||||
|
2
setup.py
2
setup.py
@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh:
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="DrissionPage",
|
name="DrissionPage",
|
||||||
version="3.2.13",
|
version="3.2.14",
|
||||||
author="g1879",
|
author="g1879",
|
||||||
author_email="g1879@qq.com",
|
author_email="g1879@qq.com",
|
||||||
description="Python based web automation tool. It can control the browser and send and receive data packets.",
|
description="Python based web automation tool. It can control the browser and send and receive data packets.",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user