Merge pull request !23 from g1879/debug
This commit is contained in:
g1879 2023-09-11 12:33:55 +00:00 committed by Gitee
commit d3c88deff6
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
8 changed files with 44 additions and 15 deletions

View File

@ -905,6 +905,7 @@ class ChromiumBaseSetter(object):
""" """
if implicit is not None: if implicit is not None:
self._page.timeouts.implicit = implicit self._page.timeouts.implicit = implicit
self._page._timeout = implicit
if page_load is not None: if page_load is not None:
self._page.timeouts.page_load = page_load self._page.timeouts.page_load = page_load

View File

@ -108,6 +108,9 @@ class ChromiumDriver(object):
continue continue
except Exception:
return None
finally: finally:
self.method_results.pop(message['id'], None) self.method_results.pop(message['id'], None)

View File

@ -121,8 +121,8 @@ class ChromiumElement(DrissionElement):
@property @property
def size(self): def size(self):
"""返回元素宽和高组成的元组""" """返回元素宽和高组成的元组"""
model = self.page.run_cdp('DOM.getBoxModel', backendNodeId=self._backend_id)['model'] border = self.page.run_cdp('DOM.getBoxModel', backendNodeId=self._backend_id)['model']['border']
return model['width'], model['height'] return int(border[2] - border[0]), int(border[5] - border[1])
@property @property
def set(self): def set(self):
@ -515,8 +515,6 @@ class ChromiumElement(DrissionElement):
while not self.run_js(js) and perf_counter() < end_time: while not self.run_js(js) and perf_counter() < end_time:
sleep(.1) sleep(.1)
self.scroll.to_see(center=True)
sleep(1)
left, top = self.location left, top = self.location
width, height = self.size width, height = self.size
left_top = (left, top) left_top = (left, top)

View File

@ -15,7 +15,6 @@ from .chromium_base import ChromiumBase, Timeout, ChromiumBaseSetter, ChromiumBa
from .chromium_driver import ChromiumDriver from .chromium_driver import ChromiumDriver
from .chromium_tab import ChromiumTab from .chromium_tab import ChromiumTab
from .commons.browser import connect_browser from .commons.browser import connect_browser
from .commons.tools import port_is_using
from .commons.web import set_session_cookies from .commons.web import set_session_cookies
from .configs.chromium_options import ChromiumOptions from .configs.chromium_options import ChromiumOptions
from .errors import CallMethodError, BrowserConnectError from .errors import CallMethodError, BrowserConnectError
@ -356,9 +355,16 @@ class ChromiumPage(ChromiumBase):
"""关闭浏览器""" """关闭浏览器"""
self._tab_obj.Browser.close() self._tab_obj.Browser.close()
self._tab_obj.stop() self._tab_obj.stop()
ip, port = self.address.split(':')
while port_is_using(ip, port): if self.process_id:
sleep(.1) 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): def _on_alert_close(self, **kwargs):
"""alert关闭时触发的方法""" """alert关闭时触发的方法"""

View File

@ -5,9 +5,10 @@
""" """
from json import load, dump from json import load, dump
from pathlib import Path from pathlib import Path
from subprocess import Popen from subprocess import Popen, DEVNULL
from tempfile import gettempdir from tempfile import gettempdir
from time import perf_counter, sleep from time import perf_counter, sleep
from platform import system
from requests import get as requests_get from requests import get as requests_get
@ -63,6 +64,7 @@ def get_launch_args(opt):
result = set() result = set()
has_user_path = False has_user_path = False
remote_allow = False remote_allow = False
headless = False
for i in opt.arguments: for i in opt.arguments:
if i.startswith(('--load-extension=', '--remote-debugging-port=')): if i.startswith(('--load-extension=', '--remote-debugging-port=')):
continue continue
@ -72,6 +74,8 @@ def get_launch_args(opt):
continue continue
elif i.startswith('--remote-allow-origins='): elif i.startswith('--remote-allow-origins='):
remote_allow = True remote_allow = True
elif i.startswith('--headless'):
headless = True
result.add(i) result.add(i)
@ -84,6 +88,12 @@ def get_launch_args(opt):
if not remote_allow: if not remote_allow:
result.add('--remote-allow-origins=*') 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) result = list(result)
# ----------处理插件extensions------------- # ----------处理插件extensions-------------
@ -167,7 +177,7 @@ def test_connect(ip, port):
def _run_browser(port, path: str, args) -> Popen: def _run_browser(port, path: str, args) -> Popen:
"""创建chrome进程 """创建chrome进程
:param port: 端口号 :param port: 端口号
:param path: 浏览器地址 :param path: 浏览器路径
:param args: 启动参数 :param args: 启动参数
:return: 进程对象 :return: 进程对象
""" """
@ -176,7 +186,7 @@ def _run_browser(port, path: str, args) -> Popen:
arguments = [p, f'--remote-debugging-port={port}'] arguments = [p, f'--remote-debugging-port={port}']
arguments.extend(args) arguments.extend(args)
try: try:
return Popen(arguments, shell=False) return Popen(arguments, shell=False, stdout=DEVNULL, stderr=DEVNULL)
except FileNotFoundError: except FileNotFoundError:
raise FileNotFoundError('未找到浏览器,请手动指定浏览器可执行文件路径。') raise FileNotFoundError('未找到浏览器,请手动指定浏览器可执行文件路径。')

View File

@ -314,7 +314,19 @@ def get_chrome_path(ini_path=None,
return str(path) return str(path)
from platform import system 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 return None
# -----------从注册表中获取-------------- # -----------从注册表中获取--------------

View File

@ -447,8 +447,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
self._response = None self._response = None
self._has_session = None self._has_session = None
if self._has_driver: if self._has_driver:
self._tab_obj.Browser.close() super(SessionPage, self).quit()
self._tab_obj.stop()
self._tab_obj = None self._tab_obj = None
self._has_driver = None self._has_driver = None

View File

@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh:
setup( setup(
name="DrissionPage", name="DrissionPage",
version="3.2.31", version="3.2.32",
author="g1879", author="g1879",
author_email="g1879@qq.com", author_email="g1879@qq.com",
description="Python based web automation tool. It can control the browser and send and receive data packets.", description="Python based web automation tool. It can control the browser and send and receive data packets.",