Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Python executable with chromedriver & Selenium

I have created a small web scraping app using Selenium & chromedriver for a project that outputs the content into an excel file. The people I did this app for unfortunately aren't the most tech-savvy.

So my question is how can I share this app with these people?

I looked into py2exe.org, but it doesn't take the chromedriver into account when creating the executable. Any better ways of doing this, without these people having to add the files manually to their "usr/bin"?

like image 373
user2868900 Avatar asked Apr 16 '18 08:04

user2868900


People also ask

How to make chromedriver executable in EXE?

It is made executable by PyInstaller, but with --add-binary as an option. This is specified when you want to include a binary file other than Python (this time chromedriver.exe) in the exe. Use the following command (replace each path): pyinstaller./main.py --onefile --noconsole --add-binary "./driver/chromedriver.exe;./driver"

How to make chromedriver executable with pyinstaller?

It is made executable by PyInstaller, but with --add-binary as an option. This is specified when you want to include a binary file other than Python (this time chromedriver.exe) in the exe. Use the following command (replace each path): --onefile It outputs as one exe. --noconsole The console is not displayed when the created exe is executed.

What is @chromedriver-Py?

chromedriver-py downloads and installs the latest chromedriver binary version for automated testing of webapps. the installer supports linux, mac and windows operating systems. this package is maintained by an automated update script on travis.

How to add WebDriver driver path in Python?

driver = webdriver.Chrome (executable_path=r'C:\Users\Administrator\Desktop\arpit\automation\chromedriver_win32\chromedriver.exe') Try to execute your @Tests as a non-root user. No need to explicitly provide the driver path in your code. just put the driver path in path environment variable as well. Python will automatically detect it.


1 Answers

You can do this with the help of pyinstaller : Below is the solution which work on Windows but pyinstaller says its capable of working on Mac OS also.

Steps are:

  1. Open Command prompt
  2. Goto project path in cmd where script is present
  3. type pyinstaller Scriptname.spec Scriptname.py (Enter y/yes if prompt on screen)
  4. The build will be at 'path to project'\dist\Scriptname

Note you need to provide the details of chromedriver in Scriptname.spec when passing the

Sample content of spec file:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['Scriptname.py'],
             pathex=['Pathofproject'],
             binaries=[('C:\\Python27\\chromedriver.exe', '**.\\selenium\\webdriver**')],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='createEVIPOrg_Automation_new',
          debug=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='**scriptname**')

You need to update the Scriptname, project path where you script lies, path of chromedriver in spec file

like image 73
thebadguy Avatar answered Oct 07 '22 01:10

thebadguy