diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index 874192f..3544771 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -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('页面连接超时。') diff --git a/DrissionPage/chromium_page.py b/DrissionPage/chromium_page.py index 7012a90..4d93ca8 100644 --- a/DrissionPage/chromium_page.py +++ b/DrissionPage/chromium_page.py @@ -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] diff --git a/DrissionPage/driver_element.py b/DrissionPage/driver_element.py index 9d2c688..c09bf2f 100644 --- a/DrissionPage/driver_element.py +++ b/DrissionPage/driver_element.py @@ -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 diff --git a/DrissionPage/mix_page.py b/DrissionPage/mix_page.py index fbbfd45..f266ecd 100644 --- a/DrissionPage/mix_page.py +++ b/DrissionPage/mix_page.py @@ -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) diff --git a/DrissionPage/session_page.py b/DrissionPage/session_page.py index b4ad6f8..d3779eb 100644 --- a/DrissionPage/session_page.py +++ b/DrissionPage/session_page.py @@ -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 diff --git a/DrissionPage/session_page.pyi b/DrissionPage/session_page.pyi new file mode 100644 index 0000000..b1b2fb7 --- /dev/null +++ b/DrissionPage/session_page.pyi @@ -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: ... diff --git a/DrissionPage/web_page.py b/DrissionPage/web_page.py index 0244d0c..304f829 100644 --- a/DrissionPage/web_page.py +++ b/DrissionPage/web_page.py @@ -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