异常捕获后释放资源
This commit is contained in:
parent
d29b3e300f
commit
88343b7761
|
@ -16,7 +16,6 @@ by UltrafunkAmsterdam (https://github.com/ultrafunkamsterdam)
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
__version__ = "3.4.7"
|
__version__ = "3.4.7"
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
@ -46,7 +45,6 @@ from .reactor import Reactor
|
||||||
from .webelement import UCWebElement
|
from .webelement import UCWebElement
|
||||||
from .webelement import WebElement
|
from .webelement import WebElement
|
||||||
|
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
"Chrome",
|
"Chrome",
|
||||||
"ChromeOptions",
|
"ChromeOptions",
|
||||||
|
@ -102,29 +100,29 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
|
||||||
debug = False
|
debug = False
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
options=None,
|
options=None,
|
||||||
user_data_dir=None,
|
user_data_dir=None,
|
||||||
driver_executable_path=None,
|
driver_executable_path=None,
|
||||||
browser_executable_path=None,
|
browser_executable_path=None,
|
||||||
port=0,
|
port=0,
|
||||||
enable_cdp_events=False,
|
enable_cdp_events=False,
|
||||||
service_args=None,
|
service_args=None,
|
||||||
service_creationflags=None,
|
service_creationflags=None,
|
||||||
desired_capabilities=None,
|
desired_capabilities=None,
|
||||||
advanced_elements=False,
|
advanced_elements=False,
|
||||||
service_log_path=None,
|
service_log_path=None,
|
||||||
keep_alive=True,
|
keep_alive=True,
|
||||||
log_level=0,
|
log_level=0,
|
||||||
headless=False,
|
headless=False,
|
||||||
version_main=None,
|
version_main=None,
|
||||||
patcher_force_close=False,
|
patcher_force_close=False,
|
||||||
suppress_welcome=True,
|
suppress_welcome=True,
|
||||||
use_subprocess=True,
|
use_subprocess=True,
|
||||||
debug=False,
|
debug=False,
|
||||||
no_sandbox=True,
|
no_sandbox=True,
|
||||||
user_multi_procs: bool = False,
|
user_multi_procs: bool = False,
|
||||||
**kw,
|
**kw,
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Creates a new instance of the chrome driver.
|
Creates a new instance of the chrome driver.
|
||||||
|
@ -331,7 +329,7 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
|
||||||
# check if an old uc.ChromeOptions is used, and extract the user data dir
|
# check if an old uc.ChromeOptions is used, and extract the user data dir
|
||||||
|
|
||||||
if hasattr(options, "user_data_dir") and getattr(
|
if hasattr(options, "user_data_dir") and getattr(
|
||||||
options, "user_data_dir", None
|
options, "user_data_dir", None
|
||||||
):
|
):
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
|
@ -369,7 +367,7 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
|
||||||
|
|
||||||
if not options.binary_location:
|
if not options.binary_location:
|
||||||
options.binary_location = (
|
options.binary_location = (
|
||||||
browser_executable_path or find_chrome_executable()
|
browser_executable_path or find_chrome_executable()
|
||||||
)
|
)
|
||||||
|
|
||||||
self._delay = 3
|
self._delay = 3
|
||||||
|
@ -405,9 +403,9 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
|
||||||
# fix exit_type flag to prevent tab-restore nag
|
# fix exit_type flag to prevent tab-restore nag
|
||||||
try:
|
try:
|
||||||
with open(
|
with open(
|
||||||
os.path.join(user_data_dir, "Default/Preferences"),
|
os.path.join(user_data_dir, "Default/Preferences"),
|
||||||
encoding="latin1",
|
encoding="latin1",
|
||||||
mode="r+",
|
mode="r+",
|
||||||
) as fs:
|
) as fs:
|
||||||
config = json.load(fs)
|
config = json.load(fs)
|
||||||
if config["profile"]["exit_type"] is not None:
|
if config["profile"]["exit_type"] is not None:
|
||||||
|
@ -449,17 +447,20 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
service = None
|
service = None
|
||||||
|
try:
|
||||||
super(Chrome, self).__init__(
|
super(Chrome, self).__init__(
|
||||||
executable_path=self.patcher.executable_path,
|
executable_path=self.patcher.executable_path,
|
||||||
port=port,
|
port=port,
|
||||||
options=options,
|
options=options,
|
||||||
service_args=service_args,
|
service_args=service_args,
|
||||||
desired_capabilities=desired_capabilities,
|
desired_capabilities=desired_capabilities,
|
||||||
service_log_path=service_log_path,
|
service_log_path=service_log_path,
|
||||||
keep_alive=keep_alive,
|
keep_alive=keep_alive,
|
||||||
service=service, # needed or the service will be re-created
|
service=service, # needed or the service will be re-created
|
||||||
)
|
)
|
||||||
|
except Exception:
|
||||||
|
self.quit()
|
||||||
|
raise
|
||||||
|
|
||||||
self.reactor = None
|
self.reactor = None
|
||||||
|
|
||||||
|
@ -658,9 +659,9 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
|
||||||
|
|
||||||
def add_cdp_listener(self, event_name, callback):
|
def add_cdp_listener(self, event_name, callback):
|
||||||
if (
|
if (
|
||||||
self.reactor
|
self.reactor
|
||||||
and self.reactor is not None
|
and self.reactor is not None
|
||||||
and isinstance(self.reactor, Reactor)
|
and isinstance(self.reactor, Reactor)
|
||||||
):
|
):
|
||||||
self.reactor.add_event_handler(event_name, callback)
|
self.reactor.add_event_handler(event_name, callback)
|
||||||
return self.reactor.handlers
|
return self.reactor.handlers
|
||||||
|
@ -735,9 +736,9 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
|
||||||
except Exception as e: # noqa
|
except Exception as e: # noqa
|
||||||
logger.debug(e, exc_info=True)
|
logger.debug(e, exc_info=True)
|
||||||
if (
|
if (
|
||||||
hasattr(self, "keep_user_data_dir")
|
hasattr(self, "keep_user_data_dir")
|
||||||
and hasattr(self, "user_data_dir")
|
and hasattr(self, "user_data_dir")
|
||||||
and not self.keep_user_data_dir
|
and not self.keep_user_data_dir
|
||||||
):
|
):
|
||||||
for _ in range(5):
|
for _ in range(5):
|
||||||
try:
|
try:
|
||||||
|
@ -766,7 +767,6 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
|
||||||
|
|
||||||
original = super().__getattribute__(item)
|
original = super().__getattribute__(item)
|
||||||
if inspect.ismethod(original) and not inspect.isclass(original):
|
if inspect.ismethod(original) and not inspect.isclass(original):
|
||||||
|
|
||||||
def newfunc(*args, **kwargs):
|
def newfunc(*args, **kwargs):
|
||||||
logger.debug(
|
logger.debug(
|
||||||
"calling %s with args %s and kwargs %s\n"
|
"calling %s with args %s and kwargs %s\n"
|
||||||
|
@ -804,9 +804,9 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
|
||||||
# needs to be a classmethod so finalize can find the reference
|
# needs to be a classmethod so finalize can find the reference
|
||||||
logger.info("ensuring close")
|
logger.info("ensuring close")
|
||||||
if (
|
if (
|
||||||
hasattr(self, "service")
|
hasattr(self, "service")
|
||||||
and hasattr(self.service, "process")
|
and hasattr(self.service, "process")
|
||||||
and hasattr(self.service.process, "kill")
|
and hasattr(self.service.process, "kill")
|
||||||
):
|
):
|
||||||
self.service.process.kill()
|
self.service.process.kill()
|
||||||
|
|
||||||
|
@ -825,11 +825,11 @@ def find_chrome_executable():
|
||||||
if IS_POSIX:
|
if IS_POSIX:
|
||||||
for item in os.environ.get("PATH").split(os.pathsep):
|
for item in os.environ.get("PATH").split(os.pathsep):
|
||||||
for subitem in (
|
for subitem in (
|
||||||
"google-chrome",
|
"google-chrome",
|
||||||
"chromium",
|
"chromium",
|
||||||
"chromium-browser",
|
"chromium-browser",
|
||||||
"chrome",
|
"chrome",
|
||||||
"google-chrome-stable",
|
"google-chrome-stable",
|
||||||
):
|
):
|
||||||
candidates.add(os.sep.join((item, subitem)))
|
candidates.add(os.sep.join((item, subitem)))
|
||||||
if "darwin" in sys.platform:
|
if "darwin" in sys.platform:
|
||||||
|
@ -841,14 +841,14 @@ def find_chrome_executable():
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
for item in map(
|
for item in map(
|
||||||
os.environ.get,
|
os.environ.get,
|
||||||
("PROGRAMFILES", "PROGRAMFILES(X86)", "LOCALAPPDATA", "PROGRAMW6432"),
|
("PROGRAMFILES", "PROGRAMFILES(X86)", "LOCALAPPDATA", "PROGRAMW6432"),
|
||||||
):
|
):
|
||||||
if item is not None:
|
if item is not None:
|
||||||
for subitem in (
|
for subitem in (
|
||||||
"Google/Chrome/Application",
|
"Google/Chrome/Application",
|
||||||
"Google/Chrome Beta/Application",
|
"Google/Chrome Beta/Application",
|
||||||
"Google/Chrome Canary/Application",
|
"Google/Chrome Canary/Application",
|
||||||
):
|
):
|
||||||
candidates.add(os.sep.join((item, subitem, "chrome.exe")))
|
candidates.add(os.sep.join((item, subitem, "chrome.exe")))
|
||||||
for candidate in candidates:
|
for candidate in candidates:
|
||||||
|
|
Loading…
Reference in New Issue