Home > Software design >  Undetected-chromedriver automatically close before Python scripts finished executing
Undetected-chromedriver automatically close before Python scripts finished executing

Time:03-06

I don't know why but my undetected-chromedriver automatically close itself (returning "Process finished with exit code 0") before my Python script if finished executing.... I believe that it happens because the Python script after the last action taken with the driver takes a long time, and undetected-chromedriver just quits after an amount of time... Could someone pls teach me what codes i should add in order to fix this problem? Thank you!

My code is as below:

import undetected_chromedriver as uc

if __name__ == "__main__":
   driver = uc.Chrome(version_main=98)
   driver.get(URL)

   for i in list:
        element_1 = WebDriverWait(driver, 10).until(
                    EC.presence_of_element_located((By.XPATH, ".//h2"))).get_attribute("innerHTML")

         AND THEN 92 MORE LINE OF PYTHON LINES OF CODES...

(my undetected chrome-driver close itself even before the first loop is finished. it ends before running any PYTHON codes...)

I also tried to add opts.add_experimental_option("detach", True) into the driver following this solution (Chromedriver closing after test) as below but then I got the error below (I also tried replacing opts = ChromeOptions() with opts = uc.ChromeOptions()):

New code:

import undetected_chromedriver as uc
from selenium.webdriver import ChromeOptions, Chrome

if __name__ == "__main__":
   opts = ChromeOptions()
   opts.add_experimental_option("detach", True)
   driver = uc.Chrome(version_main=98, options=opts)
   driver.get(URL)

   for i in list:
        element_1 = WebDriverWait(driver, 10).until(
                    EC.presence_of_element_located((By.XPATH, ".//h2"))).get_attribute("innerHTML")

         AND THEN 92 MORE LINE OF PYTHON LINES OF CODES...

Error:

Traceback (most recent call last):
  File "/Users/kienletrung/Desktop/AI Project/run.py", line 17, in <module>
    driver = uc.Chrome(version_main=98, options=opts)
  File "/Users/kienletrung/Desktop/Testing/venv/lib/python3.8/site-packages/undetected_chromedriver/__init__.py", line 369, in __init__
    super(Chrome, self).__init__(
  File "/Users/kienletrung/Desktop/Testing/venv/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 70, in __init__
    super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
  File "/Users/kienletrung/Desktop/Testing/venv/lib/python3.8/site-packages/selenium/webdriver/chromium/webdriver.py", line 93, in __init__
    RemoteWebDriver.__init__(
  File "/Users/kienletrung/Desktop/Testing/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 269, in __init__
    self.start_session(capabilities, browser_profile)
  File "/Users/kienletrung/Desktop/Testing/venv/lib/python3.8/site-packages/undetected_chromedriver/__init__.py", line 552, in start_session
    super(selenium.webdriver.chrome.webdriver.WebDriver, self).start_session(
  File "/Users/kienletrung/Desktop/Testing/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 360, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/Users/kienletrung/Desktop/Testing/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 425, in execute
    self.error_handler.check_response(response)
  File "/Users/kienletrung/Desktop/Testing/venv/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: cannot parse capability: goog:chromeOptions
from invalid argument: unrecognized chrome option: detach

FYI, I also tried using undetected chromeversion 1 (using import undetected_chromedriver._compat as uc), the detach statement works and the browser still stays there but the code (returning "Process finished with exit code 0") still appears and end my program before my Python code finished executing...

CodePudding user response:

Change

opts.add_experimental_option("detach", True)

to

opts.set_capability("detach", True)

The documentation for the detached for Chrome says:

If false, Chrome will be quit when ChromeDriver is killed, regardless of whether the session is quit. If true, Chrome will only be quit if the session is quit (or closed). Note, if true, and the session is not quit, ChromeDriver cannot clean up the temporary user data directory that the running Chrome instance is using.

Thus, this snippet above will only make the driver stay open while the session is running.

If you want to make it stay open indefinetely, you need to also keep the session running indefinetely. For that, you can add at the end of the script:

sleep(100000)

while also importing sleep at the beggining of the script:

from time import sleep

This, plus the detached, should keep your window open for your purposes.

CodePudding user response:

My program suddenly stops because of my Python codes, not because of the Chromedriver :*)

  • Related