How to Exe a Python App and Take a Google Screenshot with One Click

overview

There was a time when I wanted to take a screenshot of the browser with one click, so I will leave it as a memorandum. selenium is a handy tool that allows you to easily convert Python scripts into executables. Also, selenium is a library for manipulating browsers in Python. Here, I will explain the procedure to develop an application that bundles PyInstaller and selenium.

version check

python: v3.11.2
selenium: v4.8.2
pyinstaller: v5.8.0

Installing modules

First, install selenium. Execute the following command.

pip install selenium

Then install pyinstaller. Execute the following command.

pip install pyinstaller

download drivers

Next you will need to download ChromeDriver. Download the driver that matches your Chrome version https://chromedriver.chromium.org/downloads

Once the download is complete, save it to your driver directory

whole source code

from selenium import webdriver

# Install the Google Chrome driver
# https://sites.google.com/a/chromium.org/chromedriver/downloads
driver_path = "/driver/chromedriver"

# Start Google Chrome
driver = webdriver.Chrome(executable_path=driver_path)

# Open the top page of Google
driver.get("https://www.google.com/")

# take a screenshot
driver.save_screenshot("google.png")

# close the browser
driver. quit()

convert script

pyinstaller ./your_script.py --onefile --noconsole --path=./driver/msedgedriver.exe --add-binary './driver/msedgedriver.exe;./driver'

Here, replace your_script.py with the filename of the Python script you want to convert.

This command packs your_script.py into a single executable and outputs it to the dist directory.

Run executable

Finally, run the generated executable in /dist. Executables do not require a Python execution environment and can be easily run by others.

The above is the procedure to put a Python script with Selenium into a single file and convert it to an executable file. By using PyInstaller, you can save yourself the trouble of installing a Python environment when sharing your Python scripts with others. Please try.

Manageable project structure (bonus)

The above method works well for exe conversion, but it is inconvenient when sharing with other project members. So write npm scripts to be more concise.

  • Install node.js to use the supplemental npm scripts

Create requipments for version control

//requipments.txt
pyinstaller >= 5.8.0
selenium >= 4.8.2
// package.json
{
  "name": "pyinstaller_openpyxl",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "install": "pip install -r requirements.txt",
    "start": "python main.py",
    "build": "pyinstaller ./main.py --onefile --noconsole --path=./driver/msedgedriver.exe --add-binary './driver/msedgedriver.exe;./driver'"
  },
  "author": "",
  "license": "ISC"
}

After creating the following files, install pyinstaller and selenium using npm scripts.

npm run install

Once the installation is complete, use npm scripts to create an Exe.

npm run build

Please see below for the project https://github.com/wiblok/python/tree/main/pyInstaller_selenium