From 853f299967102bb818aa9767f95ae3c7f290d1e0 Mon Sep 17 00:00:00 2001 From: UltrafunkAmsterdam Date: Sat, 20 Mar 2021 05:28:42 +0100 Subject: [PATCH] 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. --- setup.py | 2 +- undetected_chromedriver/v2.py | 33 ++++++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/setup.py b/setup.py index 39cc6bc..3c356c6 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ from setuptools import setup setup( name="undetected-chromedriver", - version="2.1.2", + version="2.2.0", packages=["undetected_chromedriver"], install_requires=["selenium",], url="https://github.com/ultrafunkamsterdam/undetected-chromedriver", diff --git a/undetected_chromedriver/v2.py b/undetected_chromedriver/v2.py index 4e8db17..80e5f2d 100644 --- a/undetected_chromedriver/v2.py +++ b/undetected_chromedriver/v2.py @@ -31,6 +31,7 @@ whats new: """ + from __future__ import annotations import io @@ -95,7 +96,7 @@ def find_chrome_executable(): return os.path.normpath(candidate) -class Chrome(selenium.webdriver.chrome.webdriver.WebDriver): +class Chrome(object): __doc__ = ( """\ @@ -131,6 +132,7 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver): p = Patcher(target_path=executable_path) p.auto(False) + self._patcher = p self.factor = factor self.delay = delay @@ -184,14 +186,13 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver): self.browser = subprocess.Popen( self.browser_args, - close_fds="win32" in sys.platform, + # close_fds="win32" in sys.platform, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) - - selenium.webdriver.chrome.webdriver.WebDriver.__init__( - self, + + self.webdriver = selenium.webdriver.chrome.webdriver.WebDriver( executable_path=p.target_path, port=port, options=options, @@ -204,7 +205,7 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver): if options.headless: - orig_get = self.get + orig_get = self.webdriver.get def get_wrapped(*args, **kwargs): if self.execute_script("return navigator.webdriver"): @@ -237,7 +238,7 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver): ) return orig_get(*args, **kwargs) - self.get = get_wrapped + self.webdriver.get = get_wrapped if emulate_touch: 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): if not 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): """ @@ -303,7 +318,7 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver): except Exception: # noqa pass try: - super().quit() + self.webdriver.quit() except Exception: # noqa pass try: