4.0.0b7当set_headless(False)但接管了无头浏览器,将起关闭并启动新的有头浏览器;prop()改用js方式

This commit is contained in:
g1879 2023-11-13 20:23:04 +08:00
parent f850760651
commit a5e86167e2
3 changed files with 6 additions and 10 deletions

View File

@ -76,6 +76,7 @@ def get_launch_args(opt):
elif i.startswith('--headless'):
if i == '--headless=false':
headless = False
continue
elif i == '--headless':
i = '--headless=new'
headless = True

View File

@ -369,14 +369,11 @@ class ChromiumElement(DrissionElement):
:param prop: 属性名
:return: 属性值文本
"""
p = self.page.run_cdp('Runtime.getProperties', objectId=self._obj_id)['result']
for i in p:
if i['name'] == prop:
if 'value' not in i or 'value' not in i['value']:
return None
value = i['value']['value']
return format_html(value) if isinstance(value, str) else value
try:
value = self.run_js(f'return this.{prop};')
return format_html(value) if isinstance(value, str) else value
except:
return None
def run_js(self, script, *args, as_expr=False):
"""对本元素执行javascript代码

View File

@ -71,10 +71,8 @@ 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'})