ultrafunkamsterdam / undetected-chromedriver

Custom Selenium Chromedriver | Zero-Config | Passes ALL bot mitigation systems (like Distil / Imperva/ Datadadome / CloudFlare IUAM)
https://github.com/UltrafunkAmsterdam/undetected-chromedriver
GNU General Public License v3.0
9.93k stars 1.16k forks source link

undetected-chromedriver 3.1.2+ is incompatible with pyinstaller #501

Open evensonliu opened 2 years ago

evensonliu commented 2 years ago

It's ok directly run it by python codes, but failed when packaged it into a windows executable file with pyinstaller.

TEST CODES

#import undetected_chromedriver as uc
from lib2to3.pgen2 import driver
import undetected_chromedriver as uc
from selenium.webdriver.chrome.webdriver import WebDriver
import time

def create():
    return uc.Chrome()

def test():
    print('000000000000')
    driver:WebDriver = create() 
    print('1111111111111')
    driver.get('https://www.google.com')
    print('2222222222222')

    while True:
        cmd = input('>>')
        if cmd == 'q':
            break

if __name__ == '__main__':
    test()

It's good to run it directly

(base) PS D:\share\python> python .\test_uc.py
000000000000                   <-----
> Patcher:__init__
> Patcher:fetch_release_number                     
> Patcher:fetch_package
> Patcher:patch
> Patcher:patch_exe
> Patcher:gen_random_cdc
> Patcher:is_binary_patched                      
1111111111111                  <-----
2222222222222                  <-----
>>

Run as an executable file

package cmd

pyinstaller.exe --clean -F .\test_uc.py

run results, it would never end up until memory error occur(all used by it).

and it would never print('1111111111111') and print('2222222222222').

 
(base) PS D:\share\python> .\dist\test_uc.exe
000000000000
> Patcher:__init__
> 11
> auto
> Patcher:fetch_release_number
> Patcher:fetch_package
> Patcher:patch
> Patcher:patch_exe
> Patcher:gen_random_cdc
> Patcher:is_binary_patched
000000000000
> Patcher:__init__
> 11
> auto
> Patcher:fetch_release_number
> Patcher:fetch_package
> Patcher:patch
> Patcher:patch_exe
> Patcher:gen_random_cdc
> Patcher:is_binary_patched
000000000000
> Patcher:__init__
> 11
> auto
> Patcher:fetch_release_number
> Patcher:fetch_package
...

Environment

os: win10 professional, 20H2 edition hardware: image

python: Python 3.8.8 (default, Apr 13 2021, 15:08:07) [MSC v.1916 32 bit (Intel)] :: Anaconda, Inc. on win32

pyinstaller: 4.8

evensonliu commented 2 years ago

It can be work around by add freeze_support before import undetected_chromedriver .

However, I don't know why it can !

from multiprocessing import freeze_support
freeze_support()

from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.webdriver import WebDriver
import undetected_chromedriver as uc
SnailProbe commented 2 years ago

python版本3.7.9 undetected-chromedriver版本3.1.2 pyinstaller版本4.8 即使

from multiprocessing import freeze_support
freeze_support()

再或者指定参数use_subprocess

self.browser = uc.Chrome(suppress_welcome=False, options=options,use_subprocess=True,
                                        executable_path=resource_path("chromedriver.exe"))

打包后本地运行没问题,但是换台电脑就不可以了

Process Process-1: 
Traceback (most recent call last): 
  File "multiprocessing\process.py", line 297, in _bootstrap 
  File "multiprocessing\process.py", line 99, in run 
  File "undetected_chromedriver\dprocess.py", line 59, in _start_detached 
  File "subprocess.py", line 800, in __init__ 
  File "subprocess.py", line 1148, in _execute_child 
  File "subprocess.py", line 555, in list2cmdline 
TypeError: argument of type 'NoneType' is not iterable 
evensonliu commented 2 years ago

python版本3.7.9 undetected-chromedriver版本3.1.2 pyinstaller版本4.8 即使

from multiprocessing import freeze_support
freeze_support()

再或者指定参数use_subprocess

self.browser = uc.Chrome(suppress_welcome=False, options=options,use_subprocess=True,
                                        executable_path=resource_path("chromedriver.exe"))

打包后本地运行没问题,但是换台电脑就不可以了

Process Process-1: 
Traceback (most recent call last): 
  File "multiprocessing\process.py", line 297, in _bootstrap 
  File "multiprocessing\process.py", line 99, in run 
  File "undetected_chromedriver\dprocess.py", line 59, in _start_detached 
  File "subprocess.py", line 800, in __init__ 
  File "subprocess.py", line 1148, in _execute_child 
  File "subprocess.py", line 555, in list2cmdline 
TypeError: argument of type 'NoneType' is not iterable 

This is mostly like to caused by Chrome.exe not found by undetected-chromedriver if you don't specific the browser_executable_path on call uc.Chrome. You can solve it by refer to this:

https://github.com/ultrafunkamsterdam/undetected-chromedriver/issues/497

SnailProbe commented 2 years ago

python版本3.7.9 undetected-chromedriver版本3.1.2 pyinstaller版本4.8 即使

from multiprocessing import freeze_support
freeze_support()

再或者指定参数use_subprocess

self.browser = uc.Chrome(suppress_welcome=False, options=options,use_subprocess=True,
                                        executable_path=resource_path("chromedriver.exe"))

打包后本地运行没问题,但是换台电脑就不可以了

Process Process-1: 
Traceback (most recent call last): 
  File "multiprocessing\process.py", line 297, in _bootstrap 
  File "multiprocessing\process.py", line 99, in run 
  File "undetected_chromedriver\dprocess.py", line 59, in _start_detached 
  File "subprocess.py", line 800, in __init__ 
  File "subprocess.py", line 1148, in _execute_child 
  File "subprocess.py", line 555, in list2cmdline 
TypeError: argument of type 'NoneType' is not iterable 

This is mostly like to caused by Chrome.exe not found by undetected-chromedriver if you don't specific the browser_executable_path on call uc.Chrome. You can solve it by refer to this:

497

Thank you. It's been very productive

bangard commented 2 years ago

Hi, please try it on this way.

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
    return os.path.join(base_path, relative_path)
a = Analysis(
    ['myscripts.py'],
    pathex=[],
    datas=[],
    binaries=[ ('./chromedriver_win32/chromedriver.exe', '.\\selenium\\webdriver') ],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
gymshoe commented 2 years ago

Problem as described by OP remains unchanged:
pyinstaller v 5.1 python v 3.9.13 macos 12.2.1 (Interestingly, this is first report I have seen on macOS.)