remove the subclass and use delegation to access Webdrivers methods. This way an exception somewhere along your program will still ensure gracefull shutdown afterwards and fixes some other issues as well because Webdriver is now bound to the object instead of only initialized within the __init__ haeving no references at all.
This commit is contained in:
parent
b3014260ce
commit
853f299967
2
setup.py
2
setup.py
|
@ -16,7 +16,7 @@ from setuptools import setup
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="undetected-chromedriver",
|
name="undetected-chromedriver",
|
||||||
version="2.1.2",
|
version="2.2.0",
|
||||||
packages=["undetected_chromedriver"],
|
packages=["undetected_chromedriver"],
|
||||||
install_requires=["selenium",],
|
install_requires=["selenium",],
|
||||||
url="https://github.com/ultrafunkamsterdam/undetected-chromedriver",
|
url="https://github.com/ultrafunkamsterdam/undetected-chromedriver",
|
||||||
|
|
|
@ -31,6 +31,7 @@ whats new:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import io
|
import io
|
||||||
|
@ -95,7 +96,7 @@ def find_chrome_executable():
|
||||||
return os.path.normpath(candidate)
|
return os.path.normpath(candidate)
|
||||||
|
|
||||||
|
|
||||||
class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
|
class Chrome(object):
|
||||||
|
|
||||||
__doc__ = (
|
__doc__ = (
|
||||||
"""\
|
"""\
|
||||||
|
@ -131,6 +132,7 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
|
||||||
|
|
||||||
p = Patcher(target_path=executable_path)
|
p = Patcher(target_path=executable_path)
|
||||||
p.auto(False)
|
p.auto(False)
|
||||||
|
|
||||||
self._patcher = p
|
self._patcher = p
|
||||||
self.factor = factor
|
self.factor = factor
|
||||||
self.delay = delay
|
self.delay = delay
|
||||||
|
@ -184,14 +186,13 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
|
||||||
|
|
||||||
self.browser = subprocess.Popen(
|
self.browser = subprocess.Popen(
|
||||||
self.browser_args,
|
self.browser_args,
|
||||||
close_fds="win32" in sys.platform,
|
# close_fds="win32" in sys.platform,
|
||||||
stdin=subprocess.PIPE,
|
stdin=subprocess.PIPE,
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.PIPE,
|
stderr=subprocess.PIPE,
|
||||||
)
|
)
|
||||||
|
|
||||||
selenium.webdriver.chrome.webdriver.WebDriver.__init__(
|
self.webdriver = selenium.webdriver.chrome.webdriver.WebDriver(
|
||||||
self,
|
|
||||||
executable_path=p.target_path,
|
executable_path=p.target_path,
|
||||||
port=port,
|
port=port,
|
||||||
options=options,
|
options=options,
|
||||||
|
@ -204,7 +205,7 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
|
||||||
|
|
||||||
if options.headless:
|
if options.headless:
|
||||||
|
|
||||||
orig_get = self.get
|
orig_get = self.webdriver.get
|
||||||
|
|
||||||
def get_wrapped(*args, **kwargs):
|
def get_wrapped(*args, **kwargs):
|
||||||
if self.execute_script("return navigator.webdriver"):
|
if self.execute_script("return navigator.webdriver"):
|
||||||
|
@ -237,7 +238,7 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
|
||||||
)
|
)
|
||||||
return orig_get(*args, **kwargs)
|
return orig_get(*args, **kwargs)
|
||||||
|
|
||||||
self.get = get_wrapped
|
self.webdriver.get = get_wrapped
|
||||||
|
|
||||||
if emulate_touch:
|
if emulate_touch:
|
||||||
self.execute_cdp_cmd(
|
self.execute_cdp_cmd(
|
||||||
|
@ -249,11 +250,25 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
|
||||||
})"""
|
})"""
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def __getattribute__(self, attr):
|
||||||
|
try:
|
||||||
|
return object.__getattribute__(self, attr)
|
||||||
|
except AttributeError:
|
||||||
|
try:
|
||||||
|
return object.__getattribute__(self.webdriver, attr)
|
||||||
|
except AttributeError:
|
||||||
|
raise
|
||||||
|
|
||||||
|
def __dir__(self):
|
||||||
|
return object.__dir__(self) + object.__dir__(self.webdriver)
|
||||||
|
|
||||||
|
|
||||||
def start_session(self, capabilities=None, browser_profile=None):
|
def start_session(self, capabilities=None, browser_profile=None):
|
||||||
if not capabilities:
|
if not capabilities:
|
||||||
capabilities = self.options.to_capabilities()
|
capabilities = self.options.to_capabilities()
|
||||||
super().start_session(capabilities, browser_profile)
|
self.webdriver.start_session(capabilities, browser_profile)
|
||||||
|
|
||||||
|
|
||||||
def get_in(self, url: str, delay=2, factor=1):
|
def get_in(self, url: str, delay=2, factor=1):
|
||||||
"""
|
"""
|
||||||
|
@ -303,7 +318,7 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
|
||||||
except Exception: # noqa
|
except Exception: # noqa
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
super().quit()
|
self.webdriver.quit()
|
||||||
except Exception: # noqa
|
except Exception: # noqa
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue