From 41b8d3824b552fa8a28e264f5bbe3f08595298bd Mon Sep 17 00:00:00 2001 From: g1879 Date: Tue, 18 Jun 2024 10:03:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=B9=8B=E5=89=8D=E5=A3=B0?= =?UTF-8?q?=E6=98=8E=E5=BA=9F=E5=BC=83=E7=9A=84=E5=8F=82=E6=95=B0=E3=80=81?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=92=8C=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_configs/chromium_options.py | 50 +--------------------- DrissionPage/_configs/session_options.py | 11 ----- DrissionPage/_elements/chromium_element.py | 19 -------- DrissionPage/_pages/chromium_base.py | 44 ------------------- DrissionPage/_pages/chromium_frame.py | 27 ------------ DrissionPage/_pages/chromium_page.py | 29 +------------ DrissionPage/_pages/chromium_tab.py | 4 -- DrissionPage/_pages/session_page.py | 4 -- DrissionPage/_pages/web_page.py | 9 +--- DrissionPage/_units/clicker.py | 13 ------ DrissionPage/_units/setter.py | 7 --- DrissionPage/_units/waiter.py | 33 -------------- 12 files changed, 5 insertions(+), 245 deletions(-) diff --git a/DrissionPage/_configs/chromium_options.py b/DrissionPage/_configs/chromium_options.py index 0a5a9e7..86e64d2 100644 --- a/DrissionPage/_configs/chromium_options.py +++ b/DrissionPage/_configs/chromium_options.py @@ -389,7 +389,7 @@ class ChromiumOptions(object): return self def set_paths(self, browser_path=None, local_port=None, address=None, download_path=None, - user_data_path=None, cache_path=None, debugger_address=None): + user_data_path=None, cache_path=None): """快捷的路径设置函数 :param browser_path: 浏览器可执行文件路径 :param local_port: 本地端口号 @@ -399,7 +399,6 @@ class ChromiumOptions(object): :param cache_path: 缓存路径 :return: 当前对象 """ - address = address or debugger_address if browser_path is not None: self.set_browser_path(browser_path) @@ -568,50 +567,3 @@ class ChromiumOptions(object): def __repr__(self): return f'' - - # ---------------即将废弃-------------- - - @property - def debugger_address(self): - """返回浏览器地址,ip:port""" - return self._address - - @debugger_address.setter - def debugger_address(self, address): - """设置浏览器地址,格式ip:port""" - self.set_address(address) - - def set_page_load_strategy(self, value): - return self.set_load_mode(value) - - def set_headless(self, on_off=True): - """设置是否隐藏浏览器界面 - :param on_off: 开或关 - :return: 当前对象 - """ - on_off = 'new' if on_off else 'false' - return self.set_argument('--headless', on_off) - - def set_no_imgs(self, on_off=True): - """设置是否加载图片 - :param on_off: 开或关 - :return: 当前对象 - """ - 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): - """设置是否禁用js - :param on_off: 开或关 - :return: 当前对象 - """ - on_off = None if on_off else False - return self.set_argument('--disable-javascript', on_off) - - def set_mute(self, on_off=True): - """设置是否静音 - :param on_off: 开或关 - :return: 当前对象 - """ - on_off = None if on_off else False - return self.set_argument('--mute-audio', on_off) diff --git a/DrissionPage/_configs/session_options.py b/DrissionPage/_configs/session_options.py index ee9b0f8..a533e4d 100644 --- a/DrissionPage/_configs/session_options.py +++ b/DrissionPage/_configs/session_options.py @@ -457,17 +457,6 @@ class SessionOptions(object): self._adapters = [(k, i) for k, i in session.adapters.items()] return self - # --------------即将废弃--------------- - - def set_paths(self, download_path=None): - """设置默认下载路径 - :param download_path: 下载路径 - :return: 返回当前对象 - """ - if download_path is not None: - self._download_path = str(download_path) - return self - def __repr__(self): return f'' diff --git a/DrissionPage/_elements/chromium_element.py b/DrissionPage/_elements/chromium_element.py index 68f38ec..cf32848 100644 --- a/DrissionPage/_elements/chromium_element.py +++ b/DrissionPage/_elements/chromium_element.py @@ -220,25 +220,6 @@ class ChromiumElement(DrissionElement): def value(self): return self.property('value') - # -----即将废弃开始-------- - @property - def location(self): - """返回元素左上角的绝对坐标""" - return self.rect.location - - @property - def size(self): - """返回元素宽和高组成的元组""" - return self.rect.size - - def prop(self, prop): - return self.property(prop) - - def get_src(self, timeout=None, base64_to_bytes=True): - return self.src(timeout=timeout, base64_to_bytes=base64_to_bytes) - - # -----即将废弃结束-------- - def check(self, uncheck=False, by_js=False): """选中或取消选中当前元素 :param uncheck: 是否取消选中 diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index 570e11c..fe365c9 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -1122,50 +1122,6 @@ class ChromiumBase(BasePage): f.write(png) return str(path.absolute()) - # --------------------即将废弃--------------------- - - @property - def page_load_strategy(self): - return self._load_mode - - @property - def is_alive(self): - return self.states.is_alive - - @property - def is_loading(self): - """返回页面是否正在加载状态""" - return self._is_loading - - @property - def ready_state(self): - return self._ready_state - - @property - def size(self): - """返回页面总宽高,格式:(宽, 高)""" - return self.rect.size - - def get_session_storage(self, item=None): - return self.session_storage(item) - - def get_local_storage(self, item=None): - return self.local_storage(item) - - def get_cookies(self, as_dict=False, all_domains=False, all_info=False): - return self.cookies(as_dict=as_dict, all_domains=all_domains, all_info=all_info) - - def upload(self, loc_or_ele, file_paths, by_js=False): - """触发上传文件选择框并自动填入指定路径 - :param loc_or_ele: 被点击后会触发文件选择框的元素或它的定位符 - :param file_paths: 文件路径,如果上传框支持多文件,可传入列表或字符串,字符串时多个文件用回车分隔 - :param by_js: 是否用js方式点击 - :return: None - """ - self.set.upload_files(file_paths) - self.ele(loc_or_ele).click(by_js=by_js) - self.wait.upload_paths_inputted() - class Timeout(object): """用于保存d模式timeout信息的类""" diff --git a/DrissionPage/_pages/chromium_frame.py b/DrissionPage/_pages/chromium_frame.py index f2216f9..73904b4 100644 --- a/DrissionPage/_pages/chromium_frame.py +++ b/DrissionPage/_pages/chromium_frame.py @@ -343,33 +343,6 @@ class ChromiumFrame(ChromiumBase): except: return None - # ----------------即将废弃----------------- - @property - def is_alive(self): - """返回是否仍可用""" - return self.states.is_alive - - @property - def page_size(self): - """返回frame内页面尺寸,格式:(宽,, 高)""" - return self.rect.size - - @property - def size(self): - """返回frame元素大小""" - return self.frame_ele.rect.size - - @property - def location(self): - """返回frame元素左上角的绝对坐标""" - return self.frame_ele.rect.location - - @property - def locations(self): - """返回用于获取元素位置的对象""" - return self.frame_ele.rect - # ----------------即将废弃结束----------------- - def refresh(self): """刷新frame页面""" self.doc_ele.run_js('this.location.reload();') diff --git a/DrissionPage/_pages/chromium_page.py b/DrissionPage/_pages/chromium_page.py index f26754e..7bd7277 100644 --- a/DrissionPage/_pages/chromium_page.py +++ b/DrissionPage/_pages/chromium_page.py @@ -28,13 +28,12 @@ class ChromiumPage(ChromiumBase): """用于管理浏览器的类""" _PAGES = {} - def __new__(cls, addr_or_opts=None, tab_id=None, timeout=None, addr_driver_opts=None): + def __new__(cls, addr_or_opts=None, tab_id=None, timeout=None): """ :param addr_or_opts: 浏览器地址:端口、ChromiumOptions对象或端口数字(int) :param tab_id: 要控制的标签页id,不指定默认为激活的 :param timeout: 超时时间(秒) """ - addr_or_opts = addr_or_opts or addr_driver_opts opt = handle_options(addr_or_opts) is_exist, browser_id = run_browser(opt) if browser_id in cls._PAGES: @@ -50,7 +49,7 @@ class ChromiumPage(ChromiumBase): cls._PAGES[browser_id] = r return r - def __init__(self, addr_or_opts=None, tab_id=None, timeout=None, addr_driver_opts=None): + def __init__(self, addr_or_opts=None, tab_id=None, timeout=None): """ :param addr_or_opts: 浏览器地址:端口、ChromiumOptions对象或端口数字(int) :param tab_id: 要控制的标签页id,不指定默认为激活的 @@ -277,30 +276,6 @@ class ChromiumPage(ChromiumBase): def __repr__(self): return f'' - # ----------即将废弃----------- - def close_other_tabs(self, tabs_or_ids=None): - """关闭传入的标签页以外标签页,默认保留当前页。可传入多个 - :param tabs_or_ids: 要保留的标签页对象或id,可传入列表或元组,为None时保存当前页 - :return: None - """ - self.close_tabs(tabs_or_ids, True) - - @property - def tabs(self): - """返回所有标签页id组成的列表""" - return self.browser.tab_ids - - def find_tabs(self, title=None, url=None, tab_type=None, single=True): - """查找符合条件的tab,返回它们组成的列表 - :param title: 要匹配title的文本 - :param url: 要匹配url的文本 - :param tab_type: tab类型,可用列表输入多个 - :param single: 是否返回首个结果的id,为False返回所有信息 - :return: tab id或tab列表 - """ - r = self._browser.find_tabs(title, url, tab_type) - return r[0]['id'] if r and single else r - def handle_options(addr_or_opts): """设置浏览器启动属性 diff --git a/DrissionPage/_pages/chromium_tab.py b/DrissionPage/_pages/chromium_tab.py index 903ba81..d965a5d 100644 --- a/DrissionPage/_pages/chromium_tab.py +++ b/DrissionPage/_pages/chromium_tab.py @@ -399,7 +399,3 @@ class WebPageTab(SessionPage, ChromiumTab, BasePage): def __repr__(self): return f'' - - # --------即将废弃------- - def get_cookies(self, as_dict=False, all_domains=False, all_info=False): - return self.cookies(as_dict=as_dict, all_domains=all_domains, all_info=all_info) diff --git a/DrissionPage/_pages/session_page.py b/DrissionPage/_pages/session_page.py index a2d8694..4f699c8 100644 --- a/DrissionPage/_pages/session_page.py +++ b/DrissionPage/_pages/session_page.py @@ -354,10 +354,6 @@ class SessionPage(BasePage): def __repr__(self): return f'' - # ---------即将废弃--------- - def get_cookies(self, as_dict=False, all_domains=False, all_info=False): - return self.cookies(as_dict=as_dict, all_domains=all_domains, all_info=all_info) - def check_headers(kwargs, headers, arg): """检查kwargs或headers中是否有arg所示属性""" diff --git a/DrissionPage/_pages/web_page.py b/DrissionPage/_pages/web_page.py index 3a35e18..2a8ef93 100644 --- a/DrissionPage/_pages/web_page.py +++ b/DrissionPage/_pages/web_page.py @@ -17,15 +17,14 @@ from .._units.setter import WebPageSetter class WebPage(SessionPage, ChromiumPage, BasePage): """整合浏览器和request的页面类""" - def __new__(cls, mode='d', timeout=None, chromium_options=None, session_or_options=None, driver_or_options=None): + def __new__(cls, mode='d', timeout=None, chromium_options=None, session_or_options=None): """初始化函数 :param mode: 'd' 或 's',即driver模式和session模式 :param timeout: 超时时间(秒),d模式时为寻找元素时间,s模式时为连接时间,默认10秒 :param chromium_options: Driver对象,只使用s模式时应传入False :param session_or_options: Session对象或SessionOptions对象,只使用d模式时应传入False """ - opts = chromium_options or driver_or_options - return super().__new__(cls, opts) + return super().__new__(cls, chromium_options) def __init__(self, mode='d', timeout=None, chromium_options=None, session_or_options=None, driver_or_options=None): """初始化函数 @@ -431,7 +430,3 @@ class WebPage(SessionPage, ChromiumPage, BasePage): def __repr__(self): return f'' - - # -------即将废弃-------- - def get_cookies(self, as_dict=False, all_domains=False, all_info=False): - return self.cookies(as_dict=as_dict, all_domains=all_domains, all_info=all_info) diff --git a/DrissionPage/_units/clicker.py b/DrissionPage/_units/clicker.py index e0effc9..6006b4c 100644 --- a/DrissionPage/_units/clicker.py +++ b/DrissionPage/_units/clicker.py @@ -206,16 +206,3 @@ class Clicker(object): y=client_y, button=button, clickCount=count, _ignore=AlertExistsError) self._ele.owner.run_cdp('Input.dispatchMouseEvent', type='mouseReleased', x=client_x, y=client_y, button=button, _ignore=AlertExistsError) - - # -------------即将废弃-------------- - - def twice(self): - """双击元素""" - self.at(count=2) - - def multiple(self, times=2): - """多次点击 - :param times: 默认双击 - :return: None - """ - self.at(count=times) diff --git a/DrissionPage/_units/setter.py b/DrissionPage/_units/setter.py index d52fd26..9f0e443 100644 --- a/DrissionPage/_units/setter.py +++ b/DrissionPage/_units/setter.py @@ -173,13 +173,6 @@ class ChromiumBaseSetter(BasePageSetter): self._owner.run_cdp('Network.enable') self._owner.run_cdp('Network.setBlockedURLs', urls=urls) - # --------------即将废弃--------------- - - @property - def load_strategy(self): - """返回用于设置页面加载策略的对象""" - return LoadMode(self._owner) - class TabSetter(ChromiumBaseSetter): def __init__(self, owner): diff --git a/DrissionPage/_units/waiter.py b/DrissionPage/_units/waiter.py index 9f1b409..0e792bf 100644 --- a/DrissionPage/_units/waiter.py +++ b/DrissionPage/_units/waiter.py @@ -252,39 +252,6 @@ class BaseWaiter(OriginWaiter): else: return False - # -----------即将废弃----------- - - def data_packets(self, count=1, timeout=None, fix_count: bool = True): - """等待符合要求的数据包到达指定数量 - :param count: 需要捕捉的数据包数量 - :param timeout: 超时时间,为None无限等待 - :param fix_count: 是否必须满足总数要求,发生超时,为True返回False,为False返回已捕捉到的数据包 - :return: count为1时返回数据包对象,大于1时返回列表,超时且fix_count为True时返回False""" - return self._driver.listen.wait(count, timeout, fix_count) - - def load_complete(self, timeout=None, raise_err=None): - """等待页面加载完成 - :param timeout: 超时时间,为None时使用页面timeout属性 - :param raise_err: 等待失败时是否报错,为None时根据Settings设置 - :return: 是否等待成功 - """ - return self._loading(timeout=timeout, start=False, raise_err=raise_err) - - def ele_loaded(self, locator, timeout=None, raise_err=None): - """等待元素加载到DOM - :param locator: 要等待的元素,输入定位符 - :param timeout: 超时时间,默认读取页面超时时间 - :param raise_err: 等待失败时是否报错,为None时根据Settings设置 - :return: 成功返回元素对象,失败返回False - """ - ele = self._driver._ele(locator, raise_err=False, timeout=timeout) - if ele: - return ele - if raise_err is True or Settings.raise_when_wait_failed is True: - raise WaitTimeoutError(f'等待元素加载失败(等待{timeout}秒)。') - else: - return False - class TabWaiter(BaseWaiter):