chromedriver가 포함된 파이썬으로 개발한 프로그램을 실행파일로 만드는 방법입니다. 

 

 

1. pyintaller 설치

파이썬으로 개발한 프로그램을 실행파일로 만들기 위한 도구인 pyinstaller 를 설치합니다.

pip install pyinstaller

 

2. chromedriver 추가

chromedriver를 사용하여 자동 스크롤링이나 웹화면을 호출하는 경우에 chromedriver가 필요한데, 아래는 chromedriver를 추가한 프로그램입니다.

아래처럼하면 현재 OS에 설치된 크롬브라우저의 버전에 맞는 chromedriver를 다운로드하여 사용합니다.

chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
driver.maximize_window()

chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])

## 실행시 아래 오류 발생 방지 : 실행 시 아래 오류가 발생하는데 이를 방지하기 위한 부분
## USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: 시스템에 부착된 장치가 작동하지 않습니다. (0x1F)

## C:\Users\[사용자]\.wdm\drivers\chromedriver\win32\[크롬버전]110.0.5481 디렉토리 경로에 chromedriver.exe 파일이 다운로드됩니다.

예) 사용자가 admin이고 크롬버전이 110.0.5481인 경우 C:\Users\admin\.wdm\drivers\chromedriver\win32\110.0.5481 디렉토리 경로에 chromedriver.exe 파일이 다운로드됨

 

3. 실행파일 생성

sysntax : pyinstaller <python-file> --onefile --noconsole --add-binary  "<chromedriver.exe>;."

<python-file> : 실행파일을 만들고자 하는 python 파일명(파일 위치를 지정할 수 있음)

<chromedriver.exe> : chromedriver.exe 파일의 위치와 chromedriver.exe 파일명(파일 위치를 지정할 수 있음)

pyinstaller daum_login.py --onefile --noconsole --add-binary  "chromedriver.exe;."

[optioins]

--onefile : 하나의 실행파일을 생성

--noconsole : 실행파일을 실행 시 콘솔에 메시지를 출력하지 않습니다.

--add-binary : 소스위치:포함위치(chromedriver.exe;. -> chromedriver.exe 파일을 현재 디렉토리에 포함하라는 의미). linux/linux인 경우에는 세미콜론(;) 대신 콜론 문자(:)를 사용

--add-file : 소스위치:포함위치(README;. -> README 파일을 현재 디렉토리에 포함)

 

기타 옵션은 아래 URL 참조

https://pyinstaller.org/en/stable/usage.html

pyinstaller로 컴파일하면 아래와 같은 구조의 디렉토리가 생성됩니다.

dist 디렉토리에 실행파일이 생성됩니다.

C:.
├─build
│  └─daum_login
├─dist
└─__pycache__

 

4. 예제 프로그램

아래 예제는 daum에 자동 로그인하는 프로그램 예제입니다. daum 계정이 있으면 아래 내용 중 <daum 계정ID>, <계정 패스워드> 부분을 본인의 계정/패스워드로 수정하면 자동 로그인할 수 있습니다.

- 파일명 : daum_login.py

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time

chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
driver.maximize_window()

driver.get("https://logins.daum.net/accounts/signinform.do?url=https%3A%2F%2Fwww.daum.net")

## ID/Password 입력
elem = driver.find_element(By.NAME, "id")
elem.send_keys("<daum 계정ID")
elem = driver.find_element(By.NAME, "pw")
elem.send_keys("<계정 패스워드>")

## login 버튼클릭
login_btn=driver.find_element(By.ID, "loginBtn");
login_btn.click();

# 프로그램 실행 후 화면을 10초가 유지
time.sleep(10)

아래와 같이 2가지 방법 중 하나로 컴파일합니다.

## 현재 디렉토리에서 컴파일하는 방법(단, chromedriver.exe는 py 파일과 같은 폴더에 있어야 함)
## 위의 chromedriver.exe 다운로드 경로에서 복사하여 붙여넣기한다.
pyinstaller daum_login.py --onefile --noconsole --add-binary  "chromedriver.exe;."

## 전체 경로를 지정한 컴파일 방법
## py 파일이 C:\python_project\에 있고 chromedriver.exe 파일이 C:\python_project 에 있을 때
## py 파일과 chromedriver.exe 파일 위치는 각자의 위치로 지정
pyinstaller C:\python_project\daum_login.py --onefile --add-binary  "C:\python_project\chromedriver.exe;."

 

컴파일한 디렉토리 하위에 dist 폴더가 생성됩니다. 이 폴더에 실행파일이 생성됩니다.

예제에서는 C:\python_project\dist 폴더에 daum_login.exe 실행 파일이 생성됩니다.

 

<참고> 

실행파일을 실행한 후 실행이 종료되면 브라우저가 종료되는데 이를 방지하기 위한 방법으로 아래 내용을 추가했으나, 작동하지 않고 프로그램이 종료됨. 

chrome_options.add_experimental_option('detach', True) 

 

from seleniumwire import webdriver
seleniumwire 로 webdriver를 설정했으나, 역시 프로그램이 종료됨.

+ Recent posts