2

I am making an PyQt4 application where I need to use selenium. Everything works fine while development but when I exported to single file EXE, by pyinstaller and without console, it produces following traceback error:

[WinError6] The handle is invalid

This doesn't happen when I export it when console = True (in pyinstaller spec file), The error is produced only without console.

The error produced is in the following line:

driver = webdriver.Chrome(executable_path="chromedriver.exe")

My Specs:

Python : 3.4
Architecture : 64bit
Selenium : 3.6.0
Pyinstaller : 3.3
OS : Windows 10

I googled about 1 hour but couldn't find any solution :(

Ahmad Taha
  • 1,975
  • 2
  • 17
  • 27

2 Answers2

8

After a lot of research, I found a solution for the above problem.

What you just need to do is edit the file:
C:\Python34\Lib\site-packages\selenium\webdriver\common\service.py

Change the following line:

self.process = subprocess.Popen(cmd, env=self.env,
                                        close_fds=platform.system() != 'Windows',
                                        stdout=self.log_file, stderr=self.log_file)

to:

self.process = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False, creationflags=0x08000000)

This will work even while development and also after deploying to EXE.

Might be a selenium bug.

Ahmad Taha
  • 1,975
  • 2
  • 17
  • 27
  • 1
    I found that keeping the close_fds=platform.system() != 'Windows' parameter ensures that the IEDriverServer.exe process is always closed when your application is closed. This may preferred, since otherwise you end up with multiple invisible processes. – eliteproxy Jul 31 '18 at 14:46
1

I found out that pyinstaller doesn't create a copy of chromedriver.exe in the dist folder. Copying chromedriver.exe file to there solved the problem for me.

Eran Yonai
  • 11
  • 2
  • For Firefox copy: geckodriver.exe – Yehla Oct 14 '21 at 12:31
  • @Yehla, do you need to set a path to `chromedriver.exe` so it knows where to find it? Pasting it in the `dist` folder didn't make it work for me. – tazboy Aug 07 '22 at 19:01
  • @tazboy I never used `chromedriver.exe`. With `geckodriver.exe` it worked without any modification. I just copied it into my Python project folder or in the same folder where the executable of my project was located. I assume this should be the same for `chromedriver.exe`. – Yehla Aug 11 '22 at 14:57