完善配置和自动下载driver功能

This commit is contained in:
g1879 2022-01-12 14:09:15 +08:00
parent d1831de7a1
commit b69472b0fc
2 changed files with 18 additions and 18 deletions

View File

@ -690,7 +690,7 @@ def _chrome_options_to_dict(options: Union[dict, DriverOptions, Options, None, b
re_dict = dict()
attrs = ['debugger_address', 'binary_location', 'arguments', 'extensions', 'experimental_options', 'driver_path',
'timeouts', 'set_window_rect', 'page_load_strategy']
'set_window_rect', 'page_load_strategy']
options_dir = options.__dir__()
for attr in attrs:
@ -699,6 +699,12 @@ def _chrome_options_to_dict(options: Union[dict, DriverOptions, Options, None, b
except Exception:
pass
if 'timeouts' in options_dir:
timeouts = options.__getattribute__('timeouts')
timeouts['pageLoad'] /= 1000
timeouts['script'] /= 1000
re_dict['timeouts'] = timeouts
return re_dict

View File

@ -98,8 +98,6 @@ class Drission(object):
如设置了本地调试浏览器可自动接入或打开浏览器进程
"""
if self._driver is None:
# options = _dict_to_chrome_options(self._driver_options)
if not self.driver_options.debugger_address and self._proxy:
self.driver_options.add_argument(f'--proxy-server={self._proxy["http"]}')
@ -482,29 +480,25 @@ def _create_driver(chrome_path: str, driver_path: str, options: Options) -> WebD
# 若版本不对,获取对应 chromedriver 再试
except (WebDriverException, SessionNotCreatedException):
print('打开失败尝试获取driver。\n')
from .easy_set import get_match_driver
chrome_path = None if chrome_path == 'chrome.exe' else chrome_path
driver_path = get_match_driver(chrome_path=chrome_path, check_version=False, show_msg=False)
from DrissionPage.easy_set import _get_chrome_path
if chrome_path == 'chrome.exe':
chrome_path = _get_chrome_path(show_msg=False, from_ini=False, from_system_path=False)
if not chrome_path:
chrome_path = _get_chrome_path(show_msg=False, from_ini=False, from_regedit=False)
if chrome_path:
driver_path = get_match_driver(chrome_path=chrome_path, check_version=False, show_msg=True)
if driver_path:
try:
options.binary_location = chrome_path
return webdriver.Chrome(driver_path, options=options)
except Exception:
pass
# 当找不到 driver 且 chrome_path 为 None 时,说明安装的版本过高,改在系统路径中查找
elif chrome_path is None and driver_path is None:
from DrissionPage.easy_set import _get_chrome_path
chrome_path = _get_chrome_path(show_msg=False, from_ini=False, from_regedit=False)
driver_path = get_match_driver(chrome_path=chrome_path, check_version=False, show_msg=False)
if driver_path:
options.binary_location = chrome_path
try:
return webdriver.Chrome(driver_path, options=options)
except Exception:
pass
print('无法启动请检查浏览器路径或手动设置chromedriver。\n下载地址http://npm.taobao.org/mirrors/chromedriver/')
exit(0)