wkeeling / selenium-wire

Extends Selenium's Python bindings to give you the ability to inspect requests made by the browser.
MIT License
1.9k stars 254 forks source link

ca cert not found for compiled python script #310

Closed CheemaOTB closed 3 years ago

CheemaOTB commented 3 years ago

I compiled a python script that was using selenium wire so that my friend could use it but he gets this error.

[21:41:56.795873] [Task 1] An Error Occured
Exception in thread Thread-1:
Traceback (most recent call last):
  File "<frozen ssense>", line 318, in main
  File "undetected_chromedriver__init.py", line 53, in new
  File "seleniumwire\webdriver.py", line 90, in init
  File "seleniumwire\backend.py", line 35, in create
  File "seleniumwire\server.py", line 28, in init__
  File "seleniumwire\utils.py", line 95, in extract_cert_and_key
  File "pkgutil.py", line 638, in get_data
  File "PyInstaller\loader\pyimod03_importers.py", line 365, in get_data
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\ower\AppData\Local\Temp\_MEI115042\seleniumwire\ca.crt'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "threading.py", line 950, in _bootstrap_inner
  File "threading.py", line 888, in run
  File "<frozen ssense>", line 327, in main
UnboundLocalError: local variable 'driver' referenced before assignment
CheemaOTB commented 3 years ago

My dirty solution: add ca.crt and ca.key from https://github.com/wkeeling/selenium-wire/tree/master/seleniumwire in soft AppData folder

Did you put it in this directory "C:\Users\Owner\AppData\Roaming"?

binchoo commented 3 years ago

@CheemaOTB Hello, I am also hearing from some users that they are experiencing the same issue. Where should ca.crt and ca.key be placed?

binchoo commented 3 years ago

Solution

Make the Pyinstaller include seleniumwire's ca.crt and ca.key manually.

Using pyi-makespec, we can create and edit a .spec file to inject here a list of dependent static resources. (See, Using Spec Files)

For example, this code is injecting the dependant resources to the datas argument when creating an Analysis object.

from os.path import join, dirname, abspath, split
from os import sep
import glob
import seleniumwire

pkg_dir = split(seleniumwire.__file__)[0]
pkg_data = []
pkg_data.extend((file, dirname(file).split("site-packages")[1]) for file in glob.iglob(join(pkg_dir,"**{}*".format(sep)), recursive=True))

a = Analysis([f'..{os.sep}qtmain.py'],
             pathex=['F:\\repositories\\corona-virus-vaccine-reservation'],
             binaries=[],
             datas=pkg_data,
             hiddenimports=[],

(Here the actual code.)

Then, finally, we can build an executable file from the .spec file with a command like pyinstaller foobar.spec.