diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index b23cd8d..83699f8 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -905,6 +905,7 @@ class ChromiumBaseSetter(object): """ if implicit is not None: self._page.timeouts.implicit = implicit + self._page._timeout = implicit if page_load is not None: self._page.timeouts.page_load = page_load diff --git a/DrissionPage/chromium_driver.py b/DrissionPage/chromium_driver.py index fbe08e1..a9dca5d 100644 --- a/DrissionPage/chromium_driver.py +++ b/DrissionPage/chromium_driver.py @@ -108,6 +108,9 @@ class ChromiumDriver(object): continue + except Exception: + return None + finally: self.method_results.pop(message['id'], None) diff --git a/DrissionPage/chromium_element.py b/DrissionPage/chromium_element.py index 3367380..d9f507c 100644 --- a/DrissionPage/chromium_element.py +++ b/DrissionPage/chromium_element.py @@ -121,8 +121,8 @@ class ChromiumElement(DrissionElement): @property def size(self): """返回元素宽和高组成的元组""" - model = self.page.run_cdp('DOM.getBoxModel', backendNodeId=self._backend_id)['model'] - return model['width'], model['height'] + border = self.page.run_cdp('DOM.getBoxModel', backendNodeId=self._backend_id)['model']['border'] + return int(border[2] - border[0]), int(border[5] - border[1]) @property def set(self): @@ -515,8 +515,6 @@ class ChromiumElement(DrissionElement): while not self.run_js(js) and perf_counter() < end_time: sleep(.1) - self.scroll.to_see(center=True) - sleep(1) left, top = self.location width, height = self.size left_top = (left, top) diff --git a/DrissionPage/chromium_page.py b/DrissionPage/chromium_page.py index 6b9ff62..240d776 100644 --- a/DrissionPage/chromium_page.py +++ b/DrissionPage/chromium_page.py @@ -15,7 +15,6 @@ from .chromium_base import ChromiumBase, Timeout, ChromiumBaseSetter, ChromiumBa from .chromium_driver import ChromiumDriver from .chromium_tab import ChromiumTab from .commons.browser import connect_browser -from .commons.tools import port_is_using from .commons.web import set_session_cookies from .configs.chromium_options import ChromiumOptions from .errors import CallMethodError, BrowserConnectError @@ -356,9 +355,16 @@ class ChromiumPage(ChromiumBase): """关闭浏览器""" self._tab_obj.Browser.close() self._tab_obj.stop() - ip, port = self.address.split(':') - while port_is_using(ip, port): - sleep(.1) + + if self.process_id: + from os import popen + from platform import system + txt = f'tasklist | findstr {self.process_id}' if system().lower() == 'windows' \ + else f'ps -ef | grep {self.process_id}' + while True: + p = popen(txt) + if f' {self.process_id} ' not in p.read(): + break def _on_alert_close(self, **kwargs): """alert关闭时触发的方法""" diff --git a/DrissionPage/commons/browser.py b/DrissionPage/commons/browser.py index 73349aa..5c4bf4e 100644 --- a/DrissionPage/commons/browser.py +++ b/DrissionPage/commons/browser.py @@ -5,9 +5,10 @@ """ from json import load, dump from pathlib import Path -from subprocess import Popen +from subprocess import Popen, DEVNULL from tempfile import gettempdir from time import perf_counter, sleep +from platform import system from requests import get as requests_get @@ -63,6 +64,7 @@ def get_launch_args(opt): result = set() has_user_path = False remote_allow = False + headless = False for i in opt.arguments: if i.startswith(('--load-extension=', '--remote-debugging-port=')): continue @@ -72,6 +74,8 @@ def get_launch_args(opt): continue elif i.startswith('--remote-allow-origins='): remote_allow = True + elif i.startswith('--headless'): + headless = True result.add(i) @@ -84,6 +88,12 @@ def get_launch_args(opt): if not remote_allow: result.add('--remote-allow-origins=*') + if not headless and system().lower() == 'linux': + from os import popen + r = popen('systemctl list-units | grep graphical.target') + if 'graphical.target' not in r.read(): + result.add('--headless=new') + result = list(result) # ----------处理插件extensions------------- @@ -167,7 +177,7 @@ def test_connect(ip, port): def _run_browser(port, path: str, args) -> Popen: """创建chrome进程 :param port: 端口号 - :param path: 浏览器地址 + :param path: 浏览器路径 :param args: 启动参数 :return: 进程对象 """ @@ -176,7 +186,7 @@ def _run_browser(port, path: str, args) -> Popen: arguments = [p, f'--remote-debugging-port={port}'] arguments.extend(args) try: - return Popen(arguments, shell=False) + return Popen(arguments, shell=False, stdout=DEVNULL, stderr=DEVNULL) except FileNotFoundError: raise FileNotFoundError('未找到浏览器,请手动指定浏览器可执行文件路径。') diff --git a/DrissionPage/easy_set.py b/DrissionPage/easy_set.py index 98e1260..2876e0c 100644 --- a/DrissionPage/easy_set.py +++ b/DrissionPage/easy_set.py @@ -314,7 +314,19 @@ def get_chrome_path(ini_path=None, return str(path) from platform import system - if system().lower() != 'windows': + sys = system().lower() + if sys in ('macos', 'darwin'): + return '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' + + elif sys == 'linux': + paths = ('/usr/bin/google-chrome', '/opt/google/chrome/google-chrome', + '/user/lib/chromium-browser/chromium-browser') + for p in paths: + if Path(p).exists(): + return p + return None + + elif sys != 'windows': return None # -----------从注册表中获取-------------- diff --git a/DrissionPage/web_page.py b/DrissionPage/web_page.py index 71dc778..cee4c34 100644 --- a/DrissionPage/web_page.py +++ b/DrissionPage/web_page.py @@ -447,8 +447,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): self._response = None self._has_session = None if self._has_driver: - self._tab_obj.Browser.close() - self._tab_obj.stop() + super(SessionPage, self).quit() self._tab_obj = None self._has_driver = None diff --git a/setup.py b/setup.py index cccddb0..4aef9a5 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh: setup( name="DrissionPage", - version="3.2.31", + version="3.2.32", author="g1879", author_email="g1879@qq.com", description="Python based web automation tool. It can control the browser and send and receive data packets.",