mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
接管无头浏览器时判断是否需要用无头,未完成
This commit is contained in:
parent
9fb0f84507
commit
f850760651
@ -19,7 +19,7 @@ from .tools import port_is_using
|
|||||||
def connect_browser(option):
|
def connect_browser(option):
|
||||||
"""连接或启动浏览器
|
"""连接或启动浏览器
|
||||||
:param option: ChromiumOptions对象
|
:param option: ChromiumOptions对象
|
||||||
:return: None
|
:return: 返回是否接管的浏览器
|
||||||
"""
|
"""
|
||||||
debugger_address = option.debugger_address.replace('localhost', '127.0.0.1').lstrip('http://').lstrip('https://')
|
debugger_address = option.debugger_address.replace('localhost', '127.0.0.1').lstrip('http://').lstrip('https://')
|
||||||
chrome_path = option.browser_path
|
chrome_path = option.browser_path
|
||||||
@ -27,13 +27,18 @@ def connect_browser(option):
|
|||||||
ip, port = debugger_address.split(':')
|
ip, port = debugger_address.split(':')
|
||||||
if ip != '127.0.0.1' or port_is_using(ip, port) or option.is_existing_only:
|
if ip != '127.0.0.1' or port_is_using(ip, port) or option.is_existing_only:
|
||||||
test_connect(ip, port)
|
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)
|
args = get_launch_args(option)
|
||||||
set_prefs(option)
|
set_prefs(option)
|
||||||
try:
|
try:
|
||||||
debugger = _run_browser(port, chrome_path, args)
|
_run_browser(port, chrome_path, args)
|
||||||
|
|
||||||
# 传入的路径找不到,主动在ini文件、注册表、系统变量中找
|
# 传入的路径找不到,主动在ini文件、注册表、系统变量中找
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
@ -43,10 +48,10 @@ def connect_browser(option):
|
|||||||
if not chrome_path:
|
if not chrome_path:
|
||||||
raise FileNotFoundError('无法找到chrome路径,请手动配置。')
|
raise FileNotFoundError('无法找到chrome路径,请手动配置。')
|
||||||
|
|
||||||
debugger = _run_browser(port, chrome_path, args)
|
_run_browser(port, chrome_path, args)
|
||||||
|
|
||||||
test_connect(ip, port)
|
test_connect(ip, port)
|
||||||
return chrome_path, debugger
|
return False
|
||||||
|
|
||||||
|
|
||||||
def get_launch_args(opt):
|
def get_launch_args(opt):
|
||||||
@ -71,7 +76,6 @@ def get_launch_args(opt):
|
|||||||
elif i.startswith('--headless'):
|
elif i.startswith('--headless'):
|
||||||
if i == '--headless=false':
|
if i == '--headless=false':
|
||||||
headless = False
|
headless = False
|
||||||
continue
|
|
||||||
elif i == '--headless':
|
elif i == '--headless':
|
||||||
i = '--headless=new'
|
i = '--headless=new'
|
||||||
headless = True
|
headless = True
|
||||||
@ -89,13 +93,15 @@ def get_launch_args(opt):
|
|||||||
if not remote_allow:
|
if not remote_allow:
|
||||||
result.add('--remote-allow-origins=*')
|
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
|
from os import popen
|
||||||
r = popen('systemctl list-units | grep graphical.target')
|
r = popen('systemctl list-units | grep graphical.target')
|
||||||
if 'graphical.target' not in r.read():
|
if 'graphical.target' not in r.read():
|
||||||
|
headless = True
|
||||||
result.add('--headless=new')
|
result.add('--headless=new')
|
||||||
|
|
||||||
result = list(result)
|
result = list(result)
|
||||||
|
opt._headless = headless
|
||||||
|
|
||||||
# ----------处理插件extensions-------------
|
# ----------处理插件extensions-------------
|
||||||
ext = opt.extensions
|
ext = opt.extensions
|
||||||
|
@ -8,7 +8,7 @@ from typing import Union
|
|||||||
from .._configs.chromium_options import ChromiumOptions
|
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: ...
|
def get_launch_args(opt: ChromiumOptions) -> list: ...
|
||||||
|
@ -21,6 +21,7 @@ class ChromiumOptions(object):
|
|||||||
self._user_data_path = None
|
self._user_data_path = None
|
||||||
self._user = 'Default'
|
self._user = 'Default'
|
||||||
self._prefs_to_del = []
|
self._prefs_to_del = []
|
||||||
|
self._headless = None
|
||||||
|
|
||||||
if read_file is not False:
|
if read_file is not False:
|
||||||
ini_path = str(ini_path) if ini_path else None
|
ini_path = str(ini_path) if ini_path else None
|
||||||
|
@ -26,6 +26,7 @@ class ChromiumOptions(object):
|
|||||||
self._auto_port: bool = ...
|
self._auto_port: bool = ...
|
||||||
self._system_user_path: bool = ...
|
self._system_user_path: bool = ...
|
||||||
self._existing_only: bool = ...
|
self._existing_only: bool = ...
|
||||||
|
self._headless: bool = ...
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def download_path(self) -> str: ...
|
def download_path(self) -> str: ...
|
||||||
|
@ -63,7 +63,7 @@ class ChromiumPage(ChromiumBase):
|
|||||||
|
|
||||||
def _run_browser(self):
|
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',
|
ws = get(f'http://{self._driver_options.debugger_address}/json/version',
|
||||||
headers={'Connection': 'close'})
|
headers={'Connection': 'close'})
|
||||||
if not ws:
|
if not ws:
|
||||||
@ -71,6 +71,16 @@ class ChromiumPage(ChromiumBase):
|
|||||||
ws = ws.json()['webSocketDebuggerUrl'].split('/')[-1]
|
ws = ws.json()['webSocketDebuggerUrl'].split('/')[-1]
|
||||||
self._browser = Browser(self._driver_options.debugger_address, ws, self)
|
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):
|
def _d_set_runtime_settings(self):
|
||||||
"""设置运行时用到的属性"""
|
"""设置运行时用到的属性"""
|
||||||
self._timeouts = Timeout(self,
|
self._timeouts = Timeout(self,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user