diff --git a/DrissionPage/_configs/chromium_options.py b/DrissionPage/_configs/chromium_options.py index df0c21d..d49ebba 100644 --- a/DrissionPage/_configs/chromium_options.py +++ b/DrissionPage/_configs/chromium_options.py @@ -316,30 +316,78 @@ class ChromiumOptions(object): :return: 当前对象 """ if browser_path is not None: - self._binary_location = str(browser_path) - self._auto_port = False + self.set_browser_path(browser_path) if local_port is not None: - self._debugger_address = f'127.0.0.1:{local_port}' - self._auto_port = False + self.set_local_port(local_port) if debugger_address is not None: - self.debugger_address = debugger_address + self.set_debugger_address(debugger_address) if download_path is not None: - self._download_path = str(download_path) + self.set_download_path(download_path) if user_data_path is not None: - u = str(user_data_path) - self.set_argument('--user-data-dir', u) - self._user_data_path = u - self._auto_port = False + self.set_user_data_path(user_data_path) if cache_path is not None: - self.set_argument('--disk-cache-dir', str(cache_path)) + self.set_cache_path(cache_path) return self + def set_local_port(self, port): + """设置本地启动端口 + :param port: 端口号 + :return: 当前对象 + """ + self._debugger_address = f'127.0.0.1:{port}' + self._auto_port = False + return self + + def set_debugger_address(self, address): + """设置浏览器地址,格式'ip:port' + :param address: 浏览器地址 + :return: 当前对象 + """ + self.debugger_address = address + return self + + def set_browser_path(self, path): + """设置浏览器可执行文件路径 + :param path: 浏览器路径 + :return: 当前对象 + """ + self._binary_location = str(path) + self._auto_port = False + return self + + def set_download_path(self, path): + """设置下载文件保存路径 + :param path: 下载路径 + :return: 当前对象 + """ + self._download_path = str(path) + return self + + def set_user_data_path(self, path): + """设置用户文件夹路径 + :param path: 用户文件夹路径 + :return: 当前对象 + """ + u = str(path) + self.set_argument('--user-data-dir', u) + self._user_data_path = u + self._auto_port = False + return self + + def set_cache_path(self, path): + """设置缓存路径 + :param path: 缓存路径 + :return: 当前对象 + """ + self.set_argument('--disk-cache-dir', str(path)) + return self + def use_system_user_path(self, on_off=True): """设置是否使用系统安装的浏览器默认用户文件夹 :param on_off: 开或关 diff --git a/DrissionPage/_configs/chromium_options.pyi b/DrissionPage/_configs/chromium_options.pyi index bcd3a0a..17d1906 100644 --- a/DrissionPage/_configs/chromium_options.pyi +++ b/DrissionPage/_configs/chromium_options.pyi @@ -99,6 +99,18 @@ class ChromiumOptions(object): def set_page_load_strategy(self, value: str) -> ChromiumOptions: ... + def set_browser_path(self, path: Union[str, Path]) -> ChromiumOptions: ... + + def set_local_port(self, port: Union[str, int]) -> ChromiumOptions: ... + + def set_debugger_address(self, address: str) -> ChromiumOptions: ... + + def set_download_path(self, path: Union[str, Path]) -> ChromiumOptions: ... + + def set_user_data_path(self, path: Union[str, Path]) -> ChromiumOptions: ... + + def set_cache_path(self, path: Union[str, Path]) -> ChromiumOptions: ... + def set_paths(self, browser_path: Union[str, Path] = None, local_port: Union[int, str] = None, debugger_address: str = None, download_path: Union[str, Path] = None, user_data_path: Union[str, Path] = None, cache_path: Union[str, Path] = None) -> ChromiumOptions: ... diff --git a/DrissionPage/_pages/chromium_page.py b/DrissionPage/_pages/chromium_page.py index cfe8855..efea162 100644 --- a/DrissionPage/_pages/chromium_page.py +++ b/DrissionPage/_pages/chromium_page.py @@ -156,7 +156,7 @@ class ChromiumPage(ChromiumBase): """ return self._browser.find_tabs(title, url, tab_type, single) - def new_tab(self, url=None, switch_to=False): + def _new_tab(self, url=None, switch_to=False): """新建一个标签页,该标签页在最后面 :param url: 新标签页跳转到的网址 :param switch_to: 新建标签页后是否把焦点移过去 @@ -185,6 +185,14 @@ class ChromiumPage(ChromiumBase): return tid + def new_tab(self, url=None, switch_to=False): + """新建一个标签页,该标签页在最后面 + :param url: 新标签页跳转到的网址 + :param switch_to: 新建标签页后是否把焦点移过去 + :return: switch_to为False时返回新标签页对象,否则返回当前对象, + """ + return self if switch_to else ChromiumTab(self, self._new_tab(url, switch_to)) + def to_main_tab(self): """跳转到主标签页""" self.to_tab(self._main_tab) diff --git a/DrissionPage/_pages/chromium_page.pyi b/DrissionPage/_pages/chromium_page.pyi index 9a02407..15b6714 100644 --- a/DrissionPage/_pages/chromium_page.pyi +++ b/DrissionPage/_pages/chromium_page.pyi @@ -65,7 +65,9 @@ class ChromiumPage(ChromiumBase): def find_tabs(self, title: str = None, url: str = None, tab_type: Union[str, list, tuple] = None, single: bool = True) -> Union[str, List[str]]: ... - def new_tab(self, url: str = None, switch_to: bool = False) -> str: ... + def _new_tab(self, url=None, switch_to=False) -> str: ... + + def new_tab(self, url: str = None, switch_to: bool = False) -> Union[ChromiumTab, ChromiumPage]: ... def to_main_tab(self) -> None: ... diff --git a/DrissionPage/_pages/web_page.py b/DrissionPage/_pages/web_page.py index f91aa78..bc940b2 100644 --- a/DrissionPage/_pages/web_page.py +++ b/DrissionPage/_pages/web_page.py @@ -294,6 +294,14 @@ class WebPage(SessionPage, ChromiumPage, BasePage): """ return tab_id if isinstance(tab_id, WebPageTab) else WebPageTab(self, tab_id or self.tab_id) + def new_tab(self, url=None, switch_to=False): + """新建一个标签页,该标签页在最后面 + :param url: 新标签页跳转到的网址 + :param switch_to: 新建标签页后是否把焦点移过去 + :return: switch_to为False时返回新标签页对象,否则返回当前对象, + """ + return self if switch_to else WebPageTab(self, self._new_tab(url, switch_to)) + def close_driver(self): """关闭driver及浏览器""" if self._has_driver: diff --git a/DrissionPage/_pages/web_page.pyi b/DrissionPage/_pages/web_page.pyi index e05cb9f..7939f5a 100644 --- a/DrissionPage/_pages/web_page.pyi +++ b/DrissionPage/_pages/web_page.pyi @@ -121,6 +121,8 @@ class WebPage(SessionPage, ChromiumPage, BasePage): def get_tab(self, tab_id: Union[str, WebPageTab] = None) -> WebPageTab: ... + def new_tab(self, url: str = None, switch_to: bool = False) -> Union[WebPageTab, WebPage]: ... + def close_driver(self) -> None: ... def close_session(self) -> None: ...