undetected-chromedriver icon indicating copy to clipboard operation
undetected-chromedriver copied to clipboard

OSError: [WinError 6] The handle is invalid

Open cheezypotatoes opened this issue 1 year ago • 7 comments

Any idea how to fix this? I tried messing around the init but nothing changed

cheezypotatoes avatar Oct 21 '23 12:10 cheezypotatoes

Try to specify the path of chromedriver.

MovingW avatar Oct 21 '23 15:10 MovingW

Try to specify the path of chromedriver.

still wont work

cheezypotatoes avatar Oct 22 '23 01:10 cheezypotatoes

driver = uc.Chrome(options=options, use_subprocess=False)#"use_subprocess" set false
~~~
driver.close() #last line

gembleman avatar Oct 25 '23 04:10 gembleman

The same issue, I thought it was Windows Defender Firewall, but even after adding a path exception for the virtualenv it still rejected it (I ran the script in the pipenv). The Run as Administrator didn't work either. Not using add_experimental_option also didn't help. I gave up and simply used WSL Ubuntu on Windows. I had so many cases with Windows with their permissions that in my assumptions if you do not have to use it's not worth grey hair. Not sure if it is solving the problem or a way around it.

Luk-kar avatar Nov 14 '23 14:11 Luk-kar

Hi. I faced the same problem. Solution: in the \undetected_chromedriver_init_.py file, line 798 was changed to the following form:

                 try:
                    time.sleep(0.1)
                 except OSError:
                     pass

rusGoldberg avatar Dec 11 '23 22:12 rusGoldberg

The code below will cause this behavior:

from selenium import webdriver
import undetected_chromedriver as uc

l = [uc.Chrome() for i in range(3)]

[driver.get('https://www.example.com') for driver in l]


input('...')
l.clear()

If you use selenium.webdriver instead of 'uc', the exception disappears: l = [uc.Chrome() for i in range(3)] -> l = [webdriver.Chrome() for i in range(3)]

I found that if you get this WinError 6 exception, it doesn't actually delete temporary files from C:\Users\pc\AppData\Local\Temp. Therefore, to avoid this, you should close your browsers first:

import undetected_chromedriver as uc

l = [uc.Chrome() for i in range(3)]

[driver.get('https://www.example.com') for driver in l]

input('...')
[driver.close() for driver in l]
input('...')
l.clear()

jopiks-s avatar Dec 15 '23 10:12 jopiks-s

Actually, the reason the exception disappears is because of input('...'), which makes a small pause between closing chrome (it will close if there is one tab left in the browser) and deletion. This seems to be working fine:

import undetected_chromedriver as uc
import time

l = [uc.Chrome() for i in range(3)]

[driver.get('https://www.example.com') for driver in l]

input('...')
for driver in l[:]:
    driver.close()
    time.sleep(0.1)
    l.remove(driver)

jopiks-s avatar Dec 15 '23 12:12 jopiks-s