mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
change_mode()增加copy_cookies参数;调整注释
This commit is contained in:
parent
bdb8ab4d0a
commit
c5a68d6350
@ -510,7 +510,7 @@ class ChromiumBase(BasePage):
|
|||||||
print('阻止页面加载')
|
print('阻止页面加载')
|
||||||
self._tab_obj.Page.stopLoading()
|
self._tab_obj.Page.stopLoading()
|
||||||
while self.ready_state != 'complete':
|
while self.ready_state != 'complete':
|
||||||
pass
|
sleep(.1)
|
||||||
# self._get_document()
|
# self._get_document()
|
||||||
|
|
||||||
def run_cdp(self, cmd: str, **cmd_args) -> dict:
|
def run_cdp(self, cmd: str, **cmd_args) -> dict:
|
||||||
@ -615,7 +615,7 @@ class ChromiumBase(BasePage):
|
|||||||
|
|
||||||
is_timeout = not self._wait_loading(timeout)
|
is_timeout = not self._wait_loading(timeout)
|
||||||
while self.is_loading:
|
while self.is_loading:
|
||||||
pass
|
sleep(.1)
|
||||||
|
|
||||||
if is_timeout:
|
if is_timeout:
|
||||||
err = TimeoutError('页面连接超时。')
|
err = TimeoutError('页面连接超时。')
|
||||||
|
@ -278,7 +278,7 @@ class ChromiumPage(ChromiumBase):
|
|||||||
for tab in tabs:
|
for tab in tabs:
|
||||||
self._control_session.get(f'http://{self.address}/json/close/{tab}')
|
self._control_session.get(f'http://{self.address}/json/close/{tab}')
|
||||||
while len(self.tabs) != end_len:
|
while len(self.tabs) != end_len:
|
||||||
pass
|
sleep(.1)
|
||||||
|
|
||||||
if self.main_tab in tabs:
|
if self.main_tab in tabs:
|
||||||
self.main_tab = self.tabs[0]
|
self.main_tab = self.tabs[0]
|
||||||
|
@ -646,7 +646,7 @@ class DriverElement(DrissionElement):
|
|||||||
'&& arguments[0].naturalHeight > 0')
|
'&& arguments[0].naturalHeight > 0')
|
||||||
t1 = perf_counter()
|
t1 = perf_counter()
|
||||||
while not self.run_script(js) and perf_counter() - t1 < self.page.timeout:
|
while not self.run_script(js) and perf_counter() - t1 < self.page.timeout:
|
||||||
pass
|
sleep(.1)
|
||||||
|
|
||||||
if as_bytes:
|
if as_bytes:
|
||||||
return self.inner_ele.screenshot_as_png
|
return self.inner_ele.screenshot_as_png
|
||||||
|
@ -239,13 +239,14 @@ class MixPage(SessionPage, DriverPage, BasePage):
|
|||||||
"""返回 session 保存的url"""
|
"""返回 session 保存的url"""
|
||||||
return self._response.url if self._response else None
|
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
|
"""切换模式,接收's'或'd',除此以外的字符串会切换为 d 模式 \n
|
||||||
切换时会把当前模式的cookies复制到目标模式 \n
|
切换时会把当前模式的cookies复制到目标模式 \n
|
||||||
切换后,如果go是True,调用相应的get函数使访问的页面同步 \n
|
切换后,如果go是True,调用相应的get函数使访问的页面同步 \n
|
||||||
注意:s转d时,若浏览器当前网址域名和s模式不一样,必须会跳转 \n
|
注意:s转d时,若浏览器当前网址域名和s模式不一样,必须会跳转 \n
|
||||||
:param mode: 模式字符串
|
:param mode: 模式字符串
|
||||||
:param go: 是否跳转到原模式的url
|
:param go: 是否跳转到原模式的url
|
||||||
|
:param copy_cookies: 是否复制cookies到目标模式
|
||||||
"""
|
"""
|
||||||
if mode is not None and mode.lower() == self._mode:
|
if mode is not None and mode.lower() == self._mode:
|
||||||
return
|
return
|
||||||
@ -258,7 +259,8 @@ class MixPage(SessionPage, DriverPage, BasePage):
|
|||||||
self._url = None if not self._driver else self._drission.driver.current_url
|
self._url = None if not self._driver else self._drission.driver.current_url
|
||||||
|
|
||||||
if self._session_url:
|
if self._session_url:
|
||||||
self.cookies_to_driver(self._session_url)
|
if copy_cookies:
|
||||||
|
self.cookies_to_driver(self._session_url)
|
||||||
|
|
||||||
if go:
|
if go:
|
||||||
self.get(self._session_url)
|
self.get(self._session_url)
|
||||||
@ -269,7 +271,8 @@ class MixPage(SessionPage, DriverPage, BasePage):
|
|||||||
self._url = self._session_url
|
self._url = self._session_url
|
||||||
|
|
||||||
if self._driver:
|
if self._driver:
|
||||||
self.cookies_to_session()
|
if copy_cookies:
|
||||||
|
self.cookies_to_session()
|
||||||
|
|
||||||
if go and self._drission.driver.current_url.startswith('http'):
|
if go and self._drission.driver.current_url.startswith('http'):
|
||||||
self.get(self._drission.driver.current_url)
|
self.get(self._drission.driver.current_url)
|
||||||
|
@ -96,13 +96,7 @@ class SessionPage(BasePage):
|
|||||||
"""当返回内容是json格式时,返回对应的字典"""
|
"""当返回内容是json格式时,返回对应的字典"""
|
||||||
return self.response.json()
|
return self.response.json()
|
||||||
|
|
||||||
def get(self,
|
def get(self, url, show_errmsg=False, retry=None, interval=None, timeout=None, **kwargs):
|
||||||
url: str,
|
|
||||||
show_errmsg: bool = False,
|
|
||||||
retry: int = None,
|
|
||||||
interval: float = None,
|
|
||||||
timeout: float = None,
|
|
||||||
**kwargs) -> bool:
|
|
||||||
"""用get方式跳转到url \n
|
"""用get方式跳转到url \n
|
||||||
:param url: 目标url
|
:param url: 目标url
|
||||||
:param show_errmsg: 是否显示和抛出异常
|
:param show_errmsg: 是否显示和抛出异常
|
||||||
@ -287,6 +281,9 @@ class SessionPage(BasePage):
|
|||||||
if not _check_headers(kwargs, self.session.headers, 'timeout'):
|
if not _check_headers(kwargs, self.session.headers, 'timeout'):
|
||||||
kwargs['timeout'] = self.timeout
|
kwargs['timeout'] = self.timeout
|
||||||
|
|
||||||
|
if 'allow_redirects' not in kwargs:
|
||||||
|
kwargs['allow_redirects'] = False
|
||||||
|
|
||||||
r = err = None
|
r = err = None
|
||||||
retry = retry if retry is not None else self.retry_times
|
retry = retry if retry is not None else self.retry_times
|
||||||
interval = interval if interval is not None else self.retry_interval
|
interval = interval if interval is not None else self.retry_interval
|
||||||
|
36
DrissionPage/session_page.pyi
Normal file
36
DrissionPage/session_page.pyi
Normal 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: ...
|
@ -215,13 +215,14 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
|
|||||||
elif self._mode == 'd':
|
elif self._mode == 'd':
|
||||||
return super(SessionPage, self).s_eles(loc_or_str)
|
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
|
"""切换模式,接收's'或'd',除此以外的字符串会切换为 d 模式 \n
|
||||||
切换时会把当前模式的cookies复制到目标模式 \n
|
切换时会把当前模式的cookies复制到目标模式 \n
|
||||||
切换后,如果go是True,调用相应的get函数使访问的页面同步 \n
|
切换后,如果go是True,调用相应的get函数使访问的页面同步 \n
|
||||||
注意:s转d时,若浏览器当前网址域名和s模式不一样,必须会跳转 \n
|
注意:s转d时,若浏览器当前网址域名和s模式不一样,必须会跳转 \n
|
||||||
:param mode: 模式字符串
|
:param mode: 模式字符串
|
||||||
:param go: 是否跳转到原模式的url
|
:param go: 是否跳转到原模式的url
|
||||||
|
:param copy_cookies: 是否复制cookies到目标模式
|
||||||
"""
|
"""
|
||||||
if mode is not None and mode.lower() == self._mode:
|
if mode is not None and mode.lower() == self._mode:
|
||||||
return
|
return
|
||||||
@ -237,7 +238,8 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
|
|||||||
self._has_driver = True
|
self._has_driver = True
|
||||||
|
|
||||||
if self._session_url:
|
if self._session_url:
|
||||||
self.cookies_to_driver()
|
if copy_cookies:
|
||||||
|
self.cookies_to_driver()
|
||||||
|
|
||||||
if go:
|
if go:
|
||||||
self.get(self._session_url)
|
self.get(self._session_url)
|
||||||
@ -248,7 +250,8 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
|
|||||||
self._url = self._session_url
|
self._url = self._session_url
|
||||||
|
|
||||||
if self._has_driver:
|
if self._has_driver:
|
||||||
self.cookies_to_session()
|
if copy_cookies:
|
||||||
|
self.cookies_to_session()
|
||||||
|
|
||||||
if go:
|
if go:
|
||||||
url = super(SessionPage, self).url
|
url = super(SessionPage, self).url
|
||||||
|
Loading…
x
Reference in New Issue
Block a user