diff --git a/DrissionPage/chromium_driver.py b/DrissionPage/chromium_driver.py index 65449ac..711c6ad 100644 --- a/DrissionPage/chromium_driver.py +++ b/DrissionPage/chromium_driver.py @@ -38,12 +38,12 @@ class ChromiumDriver(object): :param address: 浏览器连接地址 """ self.id = tab_id + self.address = address self.type = tab_type self.debug = False self.has_alert = False self._websocket_url = f'ws://{address}/devtools/{tab_type}/{tab_id}' - self._address = address self._cur_id = 0 self._ws = None @@ -60,16 +60,6 @@ class ChromiumDriver(object): self.method_results = {} self.event_queue = Queue() - @property - def websocket_url(self): - """返回websocket连接地址""" - return self._websocket_url - - @property - def address(self): - """返回连接地址""" - return self._address - def _send(self, message, timeout=None): """发送信息到浏览器,并返回浏览器返回的信息 :param message: 发送给浏览器的数据 diff --git a/DrissionPage/chromium_driver.pyi b/DrissionPage/chromium_driver.pyi index c45c3e6..79a416a 100644 --- a/DrissionPage/chromium_driver.pyi +++ b/DrissionPage/chromium_driver.pyi @@ -17,6 +17,7 @@ class ChromiumDriver(object): _STARTED_: str _STOPPED_: str id: str + address: str type: str debug: bool has_alert: bool @@ -34,12 +35,6 @@ class ChromiumDriver(object): def __init__(self, tab_id: str, tab_type: str, address: str): ... - @property - def websocket_url(self) -> str: ... - - @property - def address(self) -> str: ... - def _send(self, message: dict, timeout: Union[int, float] = None) -> dict: ... def _recv_loop(self) -> None: ... diff --git a/DrissionPage/chromium_page.py b/DrissionPage/chromium_page.py index a45e070..dde5672 100644 --- a/DrissionPage/chromium_page.py +++ b/DrissionPage/chromium_page.py @@ -64,20 +64,24 @@ class ChromiumPage(ChromiumBase): self._init_page(tab_id) self._set_options() + self._set_chromium_options() self._get_document() self._first_run = False def _set_options(self): - """从配置中读取设置""" + """设置WebPage中与s模式共用的配置,便于WebPage覆盖掉""" self._timeouts = Timeout(self, page_load=self._driver_options.timeouts['pageLoad'], script=self._driver_options.timeouts['script'], implicit=self._driver_options.timeouts['implicit']) self._page_load_strategy = self._driver_options.page_load_strategy + self.download_set.save_path(self._download_path) + + def _set_chromium_options(self): + """设置浏览器专有的配置""" self._main_tab = self.tab_id self._alert = Alert() self._window_setter = None - self._download_path = self._driver_options.download_path def _init_page(self, tab_id=None): """新建页面、页面刷新、切换标签页后要进行的cdp参数初始化 diff --git a/DrissionPage/chromium_page.pyi b/DrissionPage/chromium_page.pyi index f8d2843..85a55e4 100644 --- a/DrissionPage/chromium_page.pyi +++ b/DrissionPage/chromium_page.pyi @@ -34,6 +34,8 @@ class ChromiumPage(ChromiumBase): def _set_options(self) -> None: ... + def _set_chromium_options(self) -> None: ... + @property def tabs_count(self) -> int: ... diff --git a/DrissionPage/configs/chromium_options.py b/DrissionPage/configs/chromium_options.py index 17f09d6..f610933 100644 --- a/DrissionPage/configs/chromium_options.py +++ b/DrissionPage/configs/chromium_options.py @@ -124,11 +124,9 @@ class ChromiumOptions(object): :return: 当前对象 """ self.remove_argument(arg) - if value is not False: arg_str = arg if value is None else f'{arg}={value}' self._arguments.append(arg_str) - return self def remove_argument(self, value): @@ -220,7 +218,7 @@ class ChromiumOptions(object): :param on_off: 开或关 :return: 当前对象 """ - on_off = True if on_off else False + on_off = None if on_off else False return self.set_argument('--headless', on_off) def set_no_imgs(self, on_off=True): @@ -228,7 +226,7 @@ class ChromiumOptions(object): :param on_off: 开或关 :return: 当前对象 """ - on_off = True if on_off else False + on_off = None if on_off else False return self.set_argument('--blink-settings=imagesEnabled=false', on_off) def set_no_js(self, on_off=True): @@ -236,7 +234,7 @@ class ChromiumOptions(object): :param on_off: 开或关 :return: 当前对象 """ - on_off = True if on_off else False + on_off = None if on_off else False return self.set_argument('--disable-javascript', on_off) def set_mute(self, on_off=True): @@ -244,7 +242,7 @@ class ChromiumOptions(object): :param on_off: 开或关 :return: 当前对象 """ - on_off = True if on_off else False + on_off = None if on_off else False return self.set_argument('--mute-audio', on_off) def set_user_agent(self, user_agent): diff --git a/DrissionPage/configs/options_manage.py b/DrissionPage/configs/options_manage.py index e1dd2ef..d184473 100644 --- a/DrissionPage/configs/options_manage.py +++ b/DrissionPage/configs/options_manage.py @@ -2,6 +2,7 @@ from configparser import RawConfigParser, NoSectionError, NoOptionError from pathlib import Path +from pprint import pprint class OptionsManager(object): @@ -100,3 +101,10 @@ class OptionsManager(object): def save_to_default(self): """保存当前配置到默认ini文件""" return self.save('default') + + def show(self): + """打印所有设置信息""" + for i in self._conf.sections(): + print(f'[{i}]') + pprint(self.get_option(i)) + print() diff --git a/DrissionPage/configs/options_manage.pyi b/DrissionPage/configs/options_manage.pyi index 51438e2..299c699 100644 --- a/DrissionPage/configs/options_manage.pyi +++ b/DrissionPage/configs/options_manage.pyi @@ -28,3 +28,5 @@ class OptionsManager(object): def save(self, path: str = None) -> str: ... def save_to_default(self) -> str: ... + + def show(self) -> None: ... diff --git a/DrissionPage/easy_set.py b/DrissionPage/easy_set.py index eb0454a..a133447 100644 --- a/DrissionPage/easy_set.py +++ b/DrissionPage/easy_set.py @@ -5,32 +5,28 @@ """ from os import popen from pathlib import Path -from pprint import pprint from re import search from typing import Union from selenium import webdriver -from .functions.tools import unzip -from .configs.options_manage import OptionsManager +from .configs.chromium_options import ChromiumOptions from .configs.driver_options import DriverOptions +from .configs.options_manage import OptionsManager from .drission import Drission +from .functions.tools import unzip from .session_page import SessionPage def show_settings(ini_path=None): - """打印ini文件内容""" - om = OptionsManager(ini_path) - print('paths:') - pprint(om.get_option('paths')) - print('\nchrome options:') - pprint(om.get_option('chrome_options')) - print('\nsession options:') - pprint(om.get_option('session_options')) + """打印ini文件内容 + :param ini_path: ini文件路径 + :return: None + """ + OptionsManager(ini_path).show() def set_paths(driver_path=None, - chrome_path=None, browser_path=None, local_port=None, debugger_address=None, @@ -41,7 +37,6 @@ def set_paths(driver_path=None, check_version=False): """快捷的路径设置函数 \n :param driver_path: chromedriver.exe路径 - :param chrome_path: chrome.exe路径 :param browser_path: 浏览器可执行文件路径 :param local_port: 本地端口号 :param debugger_address: 调试浏览器地址,例:127.0.0.1:9222 @@ -60,9 +55,6 @@ def set_paths(driver_path=None, if driver_path is not None: om.set_item('paths', 'chromedriver_path', format_path(driver_path)) - if chrome_path is not None: - om.set_item('chrome_options', 'binary_location', format_path(chrome_path)) - if browser_path is not None: om.set_item('chrome_options', 'binary_location', format_path(browser_path)) @@ -84,23 +76,18 @@ def set_paths(driver_path=None, set_argument('--disk-cache-dir', format_path(cache_path), ini_path) if check_version: - check_driver_version(format_path(driver_path), format_path(chrome_path)) + check_driver_version(format_path(driver_path), format_path(browser_path)) -def set_argument(arg, value, ini_path=None): +def set_argument(arg, value=None, ini_path=None): """设置浏览器配置argument属性 \n :param arg: 属性名 - :param value: 属性值,有值的属性传入值,没有的传入bool + :param value: 属性值,有值的属性传入值,没有的传入None :param ini_path: 要修改的ini文件路径 :return: None """ - do = DriverOptions(ini_path=ini_path) - do.remove_argument(arg) - - if value: - arg_str = arg if isinstance(value, bool) else f'{arg}={value}' - do.add_argument(arg_str) - + do = ChromiumOptions(ini_path=ini_path) + do.set_argument(arg, value) do.save() @@ -110,7 +97,7 @@ def set_headless(on_off=True, ini_path=None): :param ini_path: 要修改的ini文件路径 :return: None """ - on_off = True if on_off else False + on_off = None if on_off else False set_argument('--headless', on_off, ini_path) @@ -120,7 +107,7 @@ def set_no_imgs(on_off=True, ini_path=None): :param ini_path: 要修改的ini文件路径 :return: None """ - on_off = True if on_off else False + on_off = None if on_off else False set_argument('--blink-settings=imagesEnabled=false', on_off, ini_path) @@ -130,7 +117,7 @@ def set_no_js(on_off=True, ini_path=None): :param ini_path: 要修改的ini文件路径 :return: None """ - on_off = True if on_off else False + on_off = None if on_off else False set_argument('--disable-javascript', on_off, ini_path) @@ -140,7 +127,7 @@ def set_mute(on_off=True, ini_path=None): :param ini_path: 要修改的ini文件路径 :return: None """ - on_off = True if on_off else False + on_off = None if on_off else False set_argument('--mute-audio', on_off, ini_path) @@ -150,7 +137,7 @@ def set_user_agent(user_agent, ini_path=None): :param ini_path: 要修改的ini文件路径 :return: None """ - set_argument('user-agent', user_agent, ini_path) + set_argument('--user-agent', user_agent, ini_path) def set_proxy(proxy, ini_path=None): diff --git a/DrissionPage/easy_set.pyi b/DrissionPage/easy_set.pyi index 8c92145..cf05f44 100644 --- a/DrissionPage/easy_set.pyi +++ b/DrissionPage/easy_set.pyi @@ -21,7 +21,7 @@ def set_paths(driver_path: str = None, check_version: bool = False) -> None: ... -def set_argument(arg: str, value: Union[bool, str], ini_path: str = None) -> None: ... +def set_argument(arg: str, value: Union[bool, str] = None, ini_path: str = None) -> None: ... def set_headless(on_off: bool = True, ini_path: str = None) -> None: ... diff --git a/DrissionPage/session_page.py b/DrissionPage/session_page.py index 57bc6f0..1848018 100644 --- a/DrissionPage/session_page.py +++ b/DrissionPage/session_page.py @@ -38,15 +38,14 @@ class SessionPage(BasePage): :return: None """ if Session_or_Options is None or isinstance(Session_or_Options, SessionOptions): - options = Session_or_Options or SessionOptions() - self._set_session(options) - self._timeout = options.timeout - self._download_path = options.download_path + self._session_options = Session_or_Options or SessionOptions() + self._set_session(self._session_options) elif isinstance(Session_or_Options, Session): self._session = Session_or_Options - self._timeout = 10 - self._download_path = None + self._session_options = SessionOptions(read_file=False) + + self._set_options() def _set_session(self, opt): """根据传入字典对session进行设置 \n @@ -70,6 +69,11 @@ class SessionPage(BasePage): if attr: self._session.__setattr__(i, attr) + def _set_options(self): + """设置WebPage中与d模式共用的配置,便于WebPage覆盖掉""" + self._timeouts = self._session_options.timeout + self._download_path = self._session_options.download_path + def set_cookies(self, cookies): cookies = cookies_to_tuple(cookies) for cookie in cookies: diff --git a/DrissionPage/session_page.pyi b/DrissionPage/session_page.pyi index 95913d8..110bd29 100644 --- a/DrissionPage/session_page.pyi +++ b/DrissionPage/session_page.pyi @@ -21,6 +21,7 @@ class SessionPage(BasePage): session_or_options: Union[Session, SessionOptions] = None, timeout: float = None): self._session: Session = ... + self._session_options: SessionOptions = ... self._url: str = ... self._response: Response = ... self._download_path: str = ... @@ -34,6 +35,8 @@ class SessionPage(BasePage): def _set_session(self, opt: SessionOptions) -> None: ... + def _set_options(self) -> None: ... + def set_cookies(self, cookies: Union[RequestsCookieJar, list, tuple, str, dict]) -> None: ... def set_headers(self, headers: dict) -> None: ... diff --git a/DrissionPage/web_page.py b/DrissionPage/web_page.py index 082887f..5450fc9 100644 --- a/DrissionPage/web_page.py +++ b/DrissionPage/web_page.py @@ -110,6 +110,10 @@ class WebPage(SessionPage, ChromiumPage, BasePage): t = self._driver_options.timeouts self.set_timeouts(t['implicit'], t['pageLoad'], t['script']) + def _set_options(self): + """覆盖父类同名方法""" + pass + def __call__(self, loc_or_str, timeout=None): """在内部查找元素 \n 例:ele = page('@id=ele_id') \n diff --git a/DrissionPage/web_page.pyi b/DrissionPage/web_page.pyi index bbf630b..9175260 100644 --- a/DrissionPage/web_page.pyi +++ b/DrissionPage/web_page.pyi @@ -172,6 +172,8 @@ class WebPage(SessionPage, ChromiumPage, BasePage): def _set_both_options(self, dr_opt: Union[ChromiumDriver, DriverOptions], se_opt: Union[Session, SessionOptions, dict, bool, None]) -> None: ... + def _set_options(self) -> None: ... + def _set_driver_options(self, driver_or_Options: Union[ChromiumDriver, DriverOptions]) -> None: ... def _set_session_options(self, Session_or_Options: Union[Session, SessionOptions]) -> None: ...