Search code examples
seleniumgoogle-app-engineselenium-chromedriver

Google App Engine throwing error pointing to my use of chrome and chrome driver for selenium


I am using selenium and have the following code, I know the Traceback I get is pointing to the following code :

driverOptions = webdriver.ChromeOptions()
driverOptions.binary_location = 'Application_2/chrome.exe'
driverOptions.add_argument("--headless")
driver = webdriver.Chrome('Application_2/chromedriver_win32/chromedriver.exe', 
chrome_options=driverOptions)

However when I try to deploy to Google App Engine, I get the following Error:

Updating service [default] (this may take several minutes)...failed.
ERROR: (gcloud.app.deploy) Error Response: [9] An internal error occurred while processing 
task /app-engine-flex/flex_await_healthy/flex_await_healthy>2021-10-29T09:56:17.969Z1225.he.0: 
[2021-10-29 09:57:41 +0000] [1] [INFO] Starting gunicorn 20.1.0
[2021-10-29 09:57:41 +0000] [1] [INFO] Listening at: http://0.0.0.0:8080 (1)
[2021-10-29 09:57:41 +0000] [1] [INFO] Using worker: sync
[2021-10-29 09:57:41 +0000] [11] [INFO] Booting worker with pid: 11
[2021-10-29 09:57:44 +0000] [11] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/env/lib/python3.6/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
  worker.init_process()
File "/env/lib/python3.6/site-packages/gunicorn/workers/base.py", line 134, in init_process
  self.load_wsgi()
File "/env/lib/python3.6/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
  self.wsgi = self.app.wsgi()
File "/env/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
  self.callable = self.load()
File "/env/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
  return self.load_wsgiapp()
File "/env/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
  return util.import_app(self.app_uri)
File "/env/lib/python3.6/site-packages/gunicorn/util.py", line 359, in import_app
  mod = importlib.import_module(module)
File "/opt/python3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
  return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/vmagent/app/main.py", line 2, in <module>
  import file_upload
File "/home/vmagent/app/file_upload.py", line 5, in <module>
  import prod_class
File "/home/vmagent/app/prod_class.py", line 17, in <module>
  driver = webdriver.Chrome('Application_2/chromedriver_win32/chromedriver.exe', 
chrome_options=driverOptions)
  File "/env/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in 
__init__
self.service.start()
File "/env/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 76, in 
start
 stdin=PIPE)
File "/opt/python3.6/lib/python3.6/subprocess.py", line 729, in __init__
 restore_signals, start_new_session)
File "/opt/python3.6/lib/python3.6/subprocess.py", line 1364, in _execute_child
  raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: 'Application_2/chromedriver_win32/chromedriver.exe'
[2021-10-29 09:57:44 +0000] [11] [INFO] Worker exiting (pid: 11)
[2021-10-29 09:57:44 +0000] [1] [INFO] Shutting down: Master
[2021-10-29 09:57:44 +0000] [1] [INFO] Reason: Worker failed to boot.

Error Log On Google App engine:

'''OSError: [Errno 8] Exec format error: 'Application/chromedriver.exe'
at _execute_child (/opt/python3.9/lib/python3.9/subprocess.py:1821)
at __init__ (/opt/python3.9/lib/python3.9/subprocess.py:951)
at start (/layers/google.python.pip/pip/lib/python3.9/site- 
packages/selenium/webdriver/common/service.py:72)
at __init__ (/layers/google.python.pip/pip/lib/python3.9/site- 
packages/selenium/webdriver/chrome/webdriver.py:73)
at <module> (/workspace/prod_class.py:17)
at <module> (/workspace/file_upload.py:5)
at <module> (/workspace/main.py:2)
at _call_with_frames_removed (<frozen importlib._bootstrap>:228)
at exec_module (<frozen importlib._bootstrap_external>:850)
at _load_unlocked (<frozen importlib._bootstrap>:680)
at _find_and_load_unlocked (<frozen importlib._bootstrap>:986)
at _find_and_load (<frozen importlib._bootstrap>:1007)
at _gcd_import (<frozen importlib._bootstrap>:1030)
at import_module (/opt/python3.9/lib/python3.9/importlib/__init__.py:127)
at import_app (/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/util.py:359)
at load_wsgiapp (/layers/google.python.pip/pip/lib/python3.9/site- 
packages/gunicorn/app/wsgiapp.py:48)
at load (/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py:58)
at wsgi (/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/app/base.py:67)
at load_wsgi (/layers/google.python.pip/pip/lib/python3.9/site- 
packages/gunicorn/workers/base.py:146)
at init_process (/layers/google.python.pip/pip/lib/python3.9/site- 
packages/gunicorn/workers/base.py:134)
at spawn_worker (/layers/google.python.pip/pip/lib/python3.9/site- 
packages/gunicorn/arbiter.py:589)'''

I have tried to downlaod the latest version of chrome and chromedirver and placed them within the same folder from where they are being called yet keep rnnning into this error. I am unsure fo what to do. Any help would be appreciated.


Solution

  • You're using a .exe file (chromedriver.exe) in Linux context which is not valid.

    driverOptions = webdriver.ChromeOptions()
    driverOptions.binary_location = 'Application_2/chrome.exe' # <- Here
    driverOptions.add_argument("--headless")
    driver = webdriver.Chrome('Application_2/chromedriver_win32/chromedriver.exe', 
    chrome_options=driverOptions)
    

    You can download a Linux compatible chromedriver version going to their site. However, you wont be able to have/download it in App Engine Standard. Instead, you have to use App Engine Flex Custom Runtime, as it suits better for what you're describing, using a Docker container with chromedriver (check this quickstart for more information).

    My recomendation is to have it working inside a Docker container, and then take a look at the official documentation about how to Deploy and Test your Custom Runtime Application. You might also want to check this post with a similar scenario.