From 2ddb7aff952c841204522b4625d51ebb7a0438f7 Mon Sep 17 00:00:00 2001 From: g1879 Date: Sat, 4 Nov 2023 17:18:28 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E5=BA=94=E6=9C=80=E6=96=B0=E7=89=88?= =?UTF-8?q?=E6=B5=8F=E8=A7=88=E5=99=A8=E7=9A=84=E4=BF=AE=E6=94=B9=EF=BC=8C?= =?UTF-8?q?tabs=E4=B8=8D=E5=8C=85=E5=90=ABF12=E7=9A=84=E7=AA=97=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_base/browser.py | 4 ++-- DrissionPage/_pages/chromium_base.py | 12 +++--------- DrissionPage/_units/clicker.py | 3 +-- 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/DrissionPage/_base/browser.py b/DrissionPage/_base/browser.py index 7c8f0e9..35ed1f0 100644 --- a/DrissionPage/_base/browser.py +++ b/DrissionPage/_base/browser.py @@ -80,13 +80,13 @@ class Browser(object): def tabs_count(self): """返回标签页数量""" j = self.run_cdp('Target.getTargets')['targetInfos'] # 不要改用get,避免卡死 - return len([i for i in j if i['type'] == 'page']) + return len([i for i in j if i['type'] == 'page' and not i['url'].startswith('devtools://')]) @property def tabs(self): """返回所有标签页id组成的列表""" j = self._driver.get(f'http://{self.address}/json').json() # 不要改用cdp,因为顺序不对 - return [i['id'] for i in j if i['type'] == 'page'] + return [i['id'] for i in j if i['type'] == 'page' and not i['url'].startswith('devtools://')] @property def process_id(self): diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index 4a3da72..79a421d 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -10,8 +10,6 @@ from re import findall from threading import Thread from time import perf_counter, sleep -from requests import get - from .._base.base import BasePage from .._base.chromium_driver import ChromiumDriver from .._commons.constants import ERROR, NoneElement @@ -25,8 +23,8 @@ from .._units.network_listener import NetworkListener from .._units.screencast import Screencast from .._units.setter import ChromiumBaseSetter from .._units.waiter import ChromiumBaseWaiter -from ..errors import (ContextLossError, ElementLossError, CDPError, TabClosedError, NoRectError, BrowserConnectError, - AlertExistsError, GetDocumentError) +from ..errors import (ContextLossError, ElementLossError, CDPError, TabClosedError, NoRectError, AlertExistsError, + GetDocumentError) class ChromiumBase(BasePage): @@ -84,11 +82,7 @@ class ChromiumBase(BasePage): self._scroll = None if not tab_id: - json = get(f'http://{self.address}/json', headers={'Connection': 'close'}).json() - tab_id = [i['id'] for i in json if i['type'] == 'page'] - if not tab_id: - raise BrowserConnectError('浏览器连接失败,可能是浏览器版本原因。') - tab_id = tab_id[0] + tab_id = self.browser.tabs[0] self._driver_init(tab_id) if self.ready_state == 'complete' and self._ready_state is None: diff --git a/DrissionPage/_units/clicker.py b/DrissionPage/_units/clicker.py index c075d35..9a8659a 100644 --- a/DrissionPage/_units/clicker.py +++ b/DrissionPage/_units/clicker.py @@ -32,7 +32,7 @@ class Clicker(object): :param timeout: 模拟点击的超时时间,等待元素可见、不被遮挡、进入视口 :return: 是否点击成功 """ - if not by_js: + if not by_js: # 模拟点击 try: self._ele.scroll.to_see() can_click = False @@ -65,7 +65,6 @@ class Clicker(object): return True if Settings.raise_when_click_failed: raise CanNotClickError - return False def right(self):