3

I am using the undetected chromedriver in python selenium, my problem is that it always closes the window after ending the program.

For example I have a line of code like:

driver.get('www.google.com')

It obviously opens google but then immediately closes the window. When I use my own chromedriver, the window stays open and I can still surf on that window even when the program ends.

Any solutions?

delamain
  • 73
  • 2
  • 7
  • If any error showed up in terminal, please paste that as well. If using on jupyter, either see the console or run the code on cmd, and then view and paste the error – Hammad Aug 19 '21 at 01:03
  • 1
    Just a suggestion, until you find an answer use time.sleep() for your debugging. It will allow you to surf for the specified time. – vitaliis Aug 19 '21 at 01:25

3 Answers3

2

The original del method in the undetected chromedriver Chrome class quits the driver:

 def __del__(self):
        try:
            self.service.process.kill()
        except:  # noqa
            pass
        self.quit()

Extend the class and override the del method. Keep the original stuff and comment out the self.quit() statement:

class MyUDC(uc.Chrome):
    def __del__(self):
        try:
            self.service.process.kill()
        except:  # noqa
            pass
        # self.quit()

Now create your driver with

driver = MyUDC()
andSoOn
  • 21
  • 3
1

I simply add a time.sleep(100) function, or kill the kernel

user15558540
  • 76
  • 1
  • 2
-1

This is because the undetected chromedriver destructor terminates the chrome process when the class is destroyed.

Then you can extend the class and override the __del__ method

import undetected_chromedriver.v2 as uc

class My_Chrome(uc.Chrome):
    def __del__(self):
        pass

driver = My_Chrome()
driver.get('www.google.com')
David Buck
  • 3,752
  • 35
  • 31
  • 35
Paulo
  • 7
  • 1