diff --git a/DrissionPage/_commons/browser.py b/DrissionPage/_commons/browser.py index 6a52559..76b8d67 100644 --- a/DrissionPage/_commons/browser.py +++ b/DrissionPage/_commons/browser.py @@ -19,7 +19,7 @@ from .tools import port_is_using def connect_browser(option): """连接或启动浏览器 :param option: ChromiumOptions对象 - :return: None + :return: 返回是否接管的浏览器 """ debugger_address = option.debugger_address.replace('localhost', '127.0.0.1').lstrip('http://').lstrip('https://') chrome_path = option.browser_path @@ -27,13 +27,18 @@ def connect_browser(option): ip, port = debugger_address.split(':') if ip != '127.0.0.1' or port_is_using(ip, port) or option.is_existing_only: test_connect(ip, port) - return + option._headless = False + for i in option.arguments: + if i.startswith('--headless') and not i.endswith('=false'): + option._headless = True + break + return True # ----------创建浏览器进程---------- args = get_launch_args(option) set_prefs(option) try: - debugger = _run_browser(port, chrome_path, args) + _run_browser(port, chrome_path, args) # 传入的路径找不到,主动在ini文件、注册表、系统变量中找 except FileNotFoundError: @@ -43,10 +48,10 @@ def connect_browser(option): if not chrome_path: raise FileNotFoundError('无法找到chrome路径,请手动配置。') - debugger = _run_browser(port, chrome_path, args) + _run_browser(port, chrome_path, args) test_connect(ip, port) - return chrome_path, debugger + return False def get_launch_args(opt): @@ -71,7 +76,6 @@ def get_launch_args(opt): elif i.startswith('--headless'): if i == '--headless=false': headless = False - continue elif i == '--headless': i = '--headless=new' headless = True @@ -89,13 +93,15 @@ def get_launch_args(opt): if not remote_allow: result.add('--remote-allow-origins=*') - if headless is not None and system().lower() == 'linux': + if headless is None and system().lower() == 'linux': from os import popen r = popen('systemctl list-units | grep graphical.target') if 'graphical.target' not in r.read(): + headless = True result.add('--headless=new') result = list(result) + opt._headless = headless # ----------处理插件extensions------------- ext = opt.extensions diff --git a/DrissionPage/_commons/browser.pyi b/DrissionPage/_commons/browser.pyi index 8c041ef..9e74e87 100644 --- a/DrissionPage/_commons/browser.pyi +++ b/DrissionPage/_commons/browser.pyi @@ -8,7 +8,7 @@ from typing import Union from .._configs.chromium_options import ChromiumOptions -def connect_browser(option: ChromiumOptions) -> None: ... +def connect_browser(option: ChromiumOptions) -> bool: ... def get_launch_args(opt: ChromiumOptions) -> list: ... diff --git a/DrissionPage/_configs/chromium_options.py b/DrissionPage/_configs/chromium_options.py index 214ccd0..e5a09d0 100644 --- a/DrissionPage/_configs/chromium_options.py +++ b/DrissionPage/_configs/chromium_options.py @@ -21,6 +21,7 @@ class ChromiumOptions(object): self._user_data_path = None self._user = 'Default' self._prefs_to_del = [] + self._headless = None if read_file is not False: ini_path = str(ini_path) if ini_path else None diff --git a/DrissionPage/_configs/chromium_options.pyi b/DrissionPage/_configs/chromium_options.pyi index 161ff85..b378421 100644 --- a/DrissionPage/_configs/chromium_options.pyi +++ b/DrissionPage/_configs/chromium_options.pyi @@ -26,6 +26,7 @@ class ChromiumOptions(object): self._auto_port: bool = ... self._system_user_path: bool = ... self._existing_only: bool = ... + self._headless: bool = ... @property def download_path(self) -> str: ... diff --git a/DrissionPage/_pages/chromium_page.py b/DrissionPage/_pages/chromium_page.py index 4e60c52..b201c83 100644 --- a/DrissionPage/_pages/chromium_page.py +++ b/DrissionPage/_pages/chromium_page.py @@ -63,7 +63,7 @@ class ChromiumPage(ChromiumBase): def _run_browser(self): """连接浏览器""" - connect_browser(self._driver_options) + is_exist = connect_browser(self._driver_options) ws = get(f'http://{self._driver_options.debugger_address}/json/version', headers={'Connection': 'close'}) if not ws: @@ -71,6 +71,16 @@ class ChromiumPage(ChromiumBase): ws = ws.json()['webSocketDebuggerUrl'].split('/')[-1] self._browser = Browser(self._driver_options.debugger_address, ws, self) + print(is_exist, self._driver_options._headless, self._browser.run_cdp('Browser.getVersion')['userAgent']) + if (is_exist and self._driver_options._headless is False and + 'headless' in self._browser.run_cdp('Browser.getVersion')['userAgent'].lower()): + print('aaa') + self._browser.quit(3) + connect_browser(self._driver_options) + ws = get(f'http://{self._driver_options.debugger_address}/json/version', headers={'Connection': 'close'}) + ws = ws.json()['webSocketDebuggerUrl'].split('/')[-1] + self._browser = Browser(self._driver_options.debugger_address, ws, self) + def _d_set_runtime_settings(self): """设置运行时用到的属性""" self._timeouts = Timeout(self,