From 95717981c88a2fc7f3feb89e42fcaa2001f9ca1d Mon Sep 17 00:00:00 2001 From: g1879 Date: Wed, 6 Sep 2023 17:02:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A0=E7=95=8C=E9=9D=A2Linux=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=90=AF=E7=94=A8=E6=97=A0=E5=A4=B4=EF=BC=9BMAC?= =?UTF-8?q?=E5=92=8CLinux=E6=B7=BB=E5=8A=A0=E9=BB=98=E8=AE=A4=E6=B5=8F?= =?UTF-8?q?=E8=A7=88=E5=99=A8=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/commons/browser.py | 12 +++++++++++- DrissionPage/easy_set.py | 16 +++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/DrissionPage/commons/browser.py b/DrissionPage/commons/browser.py index 339091f..c0d42d0 100644 --- a/DrissionPage/commons/browser.py +++ b/DrissionPage/commons/browser.py @@ -8,6 +8,7 @@ from pathlib import Path 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 @@ -62,6 +63,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 @@ -71,6 +73,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) @@ -83,6 +87,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------------- @@ -164,7 +174,7 @@ def test_connect(ip, port): def _run_browser(port, path: str, args) -> Popen: """创建chrome进程 :param port: 端口号 - :param path: 浏览器地址 + :param path: 浏览器路径 :param args: 启动参数 :return: 进程对象 """ diff --git a/DrissionPage/easy_set.py b/DrissionPage/easy_set.py index d783c5b..becea0f 100644 --- a/DrissionPage/easy_set.py +++ b/DrissionPage/easy_set.py @@ -197,7 +197,19 @@ def get_chrome_path(ini_path=None, return str(path) from platform import system - if system().lower() != 'windows': + sys = system().lower() + if sys == 'macos': + 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 # -----------从注册表中获取-------------- @@ -207,8 +219,6 @@ def get_chrome_path(ini_path=None, key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe', reserved=0, access=winreg.KEY_READ) - # key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Google\Chrome\BLBeacon\version', - # reserved=0, access=winreg.KEY_READ) k = winreg.EnumValue(key, 0) winreg.CloseKey(key)