mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
Pre Merge pull request !23 from g1879/debug
This commit is contained in:
commit
69c4c0f54e
@ -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
|
||||
|
@ -108,6 +108,9 @@ class ChromiumDriver(object):
|
||||
|
||||
continue
|
||||
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
finally:
|
||||
self.method_results.pop(message['id'], None)
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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关闭时触发的方法"""
|
||||
|
@ -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('未找到浏览器,请手动指定浏览器可执行文件路径。')
|
||||
|
||||
|
@ -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
|
||||
|
||||
# -----------从注册表中获取--------------
|
||||
|
@ -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
|
||||
|
||||
|
2
setup.py
2
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.",
|
||||
|
Loading…
x
Reference in New Issue
Block a user