change_mode()增加copy_cookies参数;调整注释

This commit is contained in:
g1879 2022-12-12 00:12:18 +08:00
parent bdb8ab4d0a
commit c5a68d6350
7 changed files with 57 additions and 18 deletions

View File

@ -510,7 +510,7 @@ class ChromiumBase(BasePage):
print('阻止页面加载')
self._tab_obj.Page.stopLoading()
while self.ready_state != 'complete':
pass
sleep(.1)
# self._get_document()
def run_cdp(self, cmd: str, **cmd_args) -> dict:
@ -615,7 +615,7 @@ class ChromiumBase(BasePage):
is_timeout = not self._wait_loading(timeout)
while self.is_loading:
pass
sleep(.1)
if is_timeout:
err = TimeoutError('页面连接超时。')

View File

@ -278,7 +278,7 @@ class ChromiumPage(ChromiumBase):
for tab in tabs:
self._control_session.get(f'http://{self.address}/json/close/{tab}')
while len(self.tabs) != end_len:
pass
sleep(.1)
if self.main_tab in tabs:
self.main_tab = self.tabs[0]

View File

@ -646,7 +646,7 @@ class DriverElement(DrissionElement):
'&& arguments[0].naturalHeight > 0')
t1 = perf_counter()
while not self.run_script(js) and perf_counter() - t1 < self.page.timeout:
pass
sleep(.1)
if as_bytes:
return self.inner_ele.screenshot_as_png

View File

@ -239,13 +239,14 @@ class MixPage(SessionPage, DriverPage, BasePage):
"""返回 session 保存的url"""
return self._response.url if self._response else None
def change_mode(self, mode: str = None, go: bool = True) -> None:
def change_mode(self, mode: str = None, go: bool = True, copy_cookies: bool = True) -> None:
"""切换模式,接收's''d',除此以外的字符串会切换为 d 模式 \n
切换时会把当前模式的cookies复制到目标模式 \n
切换时会把当前模式的cookies复制到目标模式 \n
切换后如果go是True调用相应的get函数使访问的页面同步 \n
注意s转d时若浏览器当前网址域名和s模式不一样必须会跳转 \n
:param mode: 模式字符串
:param go: 是否跳转到原模式的url
:param copy_cookies: 是否复制cookies到目标模式
"""
if mode is not None and mode.lower() == self._mode:
return
@ -258,7 +259,8 @@ class MixPage(SessionPage, DriverPage, BasePage):
self._url = None if not self._driver else self._drission.driver.current_url
if self._session_url:
self.cookies_to_driver(self._session_url)
if copy_cookies:
self.cookies_to_driver(self._session_url)
if go:
self.get(self._session_url)
@ -269,7 +271,8 @@ class MixPage(SessionPage, DriverPage, BasePage):
self._url = self._session_url
if self._driver:
self.cookies_to_session()
if copy_cookies:
self.cookies_to_session()
if go and self._drission.driver.current_url.startswith('http'):
self.get(self._drission.driver.current_url)

View File

@ -96,13 +96,7 @@ class SessionPage(BasePage):
"""当返回内容是json格式时返回对应的字典"""
return self.response.json()
def get(self,
url: str,
show_errmsg: bool = False,
retry: int = None,
interval: float = None,
timeout: float = None,
**kwargs) -> bool:
def get(self, url, show_errmsg=False, retry=None, interval=None, timeout=None, **kwargs):
"""用get方式跳转到url \n
:param url: 目标url
:param show_errmsg: 是否显示和抛出异常
@ -287,6 +281,9 @@ class SessionPage(BasePage):
if not _check_headers(kwargs, self.session.headers, 'timeout'):
kwargs['timeout'] = self.timeout
if 'allow_redirects' not in kwargs:
kwargs['allow_redirects'] = False
r = err = None
retry = retry if retry is not None else self.retry_times
interval = interval if interval is not None else self.retry_interval

View File

@ -0,0 +1,36 @@
from typing import Any, Union
from requests import Session
from .config import SessionOptions
class SessionPage:
def get(self,
url: str,
show_errmsg: bool | None = ...,
retry: int | None = ...,
interval: float | None = ...,
timeout: float | None = ...,
params: dict | None = ...,
data: Union[dict, str, None] = ...,
headers: dict | None = ...,
cookies: Any | None = ...,
files: Any | None = ...,
auth: Any | None = ...,
allow_redirects: bool = ...,
proxies: Any | None = ...,
hooks: Any | None = ...,
stream: Any | None = ...,
verify: Any | None = ...,
cert: Any | None = ...,
json: Union[dict, str, None] = ...,
) -> bool: ...
@property
def url(self) -> str: ...
@property
def html(self) -> str: ...
def _set_session(self, Session_or_Options: Union[Session, SessionOptions]) -> None: ...

View File

@ -215,13 +215,14 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
elif self._mode == 'd':
return super(SessionPage, self).s_eles(loc_or_str)
def change_mode(self, mode: str = None, go: bool = True) -> None:
def change_mode(self, mode: str = None, go: bool = True, copy_cookies: bool = True) -> None:
"""切换模式,接收's''d',除此以外的字符串会切换为 d 模式 \n
切换时会把当前模式的cookies复制到目标模式 \n
切换后如果go是True调用相应的get函数使访问的页面同步 \n
注意s转d时若浏览器当前网址域名和s模式不一样必须会跳转 \n
:param mode: 模式字符串
:param go: 是否跳转到原模式的url
:param copy_cookies: 是否复制cookies到目标模式
"""
if mode is not None and mode.lower() == self._mode:
return
@ -237,7 +238,8 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
self._has_driver = True
if self._session_url:
self.cookies_to_driver()
if copy_cookies:
self.cookies_to_driver()
if go:
self.get(self._session_url)
@ -248,7 +250,8 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
self._url = self._session_url
if self._has_driver:
self.cookies_to_session()
if copy_cookies:
self.cookies_to_session()
if go:
url = super(SessionPage, self).url