diff --git a/src/undetected_chromedriver/__init__.py b/src/undetected_chromedriver/__init__.py index dcf1a26..b78f60e 100644 --- a/src/undetected_chromedriver/__init__.py +++ b/src/undetected_chromedriver/__init__.py @@ -471,7 +471,7 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver): self.patcher.executable_path ) - super(Chrome, self).__init__( + super().__init__( service=service, options=options, keep_alive=keep_alive, @@ -727,10 +727,8 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver): def start_session(self, capabilities=None, browser_profile=None): if not capabilities: capabilities = self.options.to_capabilities() - super(selenium.webdriver.chrome.webdriver.WebDriver, self).start_session( - capabilities - ) - # super(Chrome, self).start_session(capabilities, browser_profile) + super().start_session(capabilities) + # super(Chrome, self).start_session(capabilities, browser_profile) # Original explicit call commented out def find_elements_recursive(self, by, value): """ diff --git a/src/undetected_chromedriver/patcher.py b/src/undetected_chromedriver/patcher.py index e8a0e96..a1dd66a 100644 --- a/src/undetected_chromedriver/patcher.py +++ b/src/undetected_chromedriver/patcher.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # this module is part of undetected_chromedriver -from distutils.version import LooseVersion +from packaging.version import Version as LooseVersion import io import json import logging @@ -12,6 +12,7 @@ import random import re import shutil import string +import subprocess import sys import time from urllib.request import urlopen @@ -373,10 +374,31 @@ class Patcher(object): """ exe_name = os.path.basename(exe_name) if IS_POSIX: - r = os.system("kill -f -9 $(pidof %s)" % exe_name) + # Using shell=True for pidof, consider a more robust pid finding method if issues arise. + # pgrep can be an alternative: ["pgrep", "-f", exe_name] + # Or psutil if adding a dependency is acceptable. + command = f"pidof {exe_name}" + try: + result = subprocess.run(command, shell=True, capture_output=True, text=True, check=True) + pids = result.stdout.strip().split() + if pids: + subprocess.run(["kill", "-9"] + pids, check=False) # Changed from -f -9 to -9 as -f is not standard for kill + return True + return False # No PIDs found + except subprocess.CalledProcessError: # pidof returns 1 if no process found + return False # No process found + except Exception as e: + logger.debug(f"Error killing process on POSIX: {e}") + return False else: - r = os.system("taskkill /f /im %s" % exe_name) - return not r + try: + # TASKKILL /F /IM chromedriver.exe + result = subprocess.run(["taskkill", "/f", "/im", exe_name], check=False, capture_output=True) + # taskkill returns 0 if process was killed, 128 if not found. + return result.returncode == 0 + except Exception as e: + logger.debug(f"Error killing process on Windows: {e}") + return False @staticmethod def gen_random_cdc():