[python] 파이썬 셀레니엄을 활용한 자동화 프로그램

파이썬 셀레니엄을 활용한 자동화 프로그램 

Modern Web Automation With Python and Selenium

python과 selenium를 활용하여 정시에 시작 시작하여 자동으로 로그인 페이지 이동 시간대를 확인하여 예약하는 자동 매크로 개발하면서 간략한 문법 설명과 배포까지의 과정을 정리하였다.


목차

개발환경

  • Windows 10 
  • Visual Studio Code 1.57
  • Python 3.9.5 64
    • selenium
    • datetime
    • chromedriver-autoinstaller
    • pyinstaller



    [Serializable]
    public partial class AccountLoginResult : BaseResult
    {
        public AccountLogin loginInfo;
        public bool needNick;
        public FirstLoginInfo firstInfo;
        public VersionInfo version;
        public long server_ticks;
        public long time_zone;
    }


    [Serializable]
    public partial class AccountLoginResult : BaseResult
    {
        public AccountLogin loginInfo;
        public bool needNick;
        public FirstLoginInfo firstInfo;
        public VersionInfo version;
        public long server_ticks;
        public long time_zone;
    }

파이썬 설치

설치 경로에서 윈도우즈용으로 다른로드 받아 아래 "Add Python 3.9 to PATH"(윈도우즈 패스 설정 자동설정)를 체크 하고 설치 한다.

설치확인을 위해 아래 터미널 창에서 python -V를 입력해 본다.

    C:\> python -V
    Python 3.9.5


프로그램 사용에 필요한 라이브러리 설치

    C:\> pip install --upgrade pip
    C:\> pip install selenium
    C:\> pip install datetime
    C:\> pip install chromedriver-autoinstaller
    C:\> pip install pyinstaller



또는 소스코드에 포함되어 있는 설치 목록 실행
    
    C:\> pip install -r .\requirements.txt 


기본 코드 정리

크롬 드라이버 초기화 & 옵션 설정

초기화코드는 실행파일에서 실행과 소스를 바로 실행하는 경우를 가정하여 두 가지로 나눠 작성하였다.

    
    import os, sys
    from selenium import webdriver

    # 실행파일로 실행시 포함된 임시폴더가 있다면
    print('find chromedriver')
    if getattr(sys, 'frozen'False):
        # 시스템 임시폴더 경와 빌드시 포함시킨 바이너리파일 경로를 더한다
        # ex)
    # > pyinstaller .\src\automation.py --F --add-binary ".\91\chromedriver.exe;.\91\"
        # > ...: C:\...\VSCode\python_selenium_automation\91\chromedriver.exe
        chromedriver_path = os.path.join(sys._MEIPASS, '.\91\chromedriver.exe')
        print(f' Absolute path to chromedriver: {chromedriver_path}')
    else:
        # 소스 실행시 크롬 버전에 맞는 크롬 드라이버를 찾고 없다면 
        # 다운로드 받아 해당 경로를 반환한다.
        # ex)
        # python automation.py
        # or
        # Ctrl + F5
        # > '.\91\chromedriver.exe'

        # 여기서 import 한 이유는 실행파일 실행시 
        # chromedriver_autoinstaller 모듈을 찾을 수 없어 에러가 발생한다.
        # 그래서 소스 직접 실행시만 import 하도록 하기 위함이다.
        # C:\...\VSCode\python_selenium_automation\dist\automation.exe
        # Traceback (most recent call last):
        #   File "automation.py", line 3, in <module>
        # ModuleNotFoundError: No module named 'chromedriver_autoinstaller'
        # [18120] Failed to execute script automation

        import chromedriver_autoinstaller
        # 현재 크롬 버전 기준 '.\91\chromedriver.exe' 생성
        chromedriver_path = chromedriver_autoinstaller.install(True
        print(f' Relative path to chromedriver: {chromedriver_path}')
        
    # webdriver options 설정 
    options = webdriver.ChromeOptions()             
    #options.add_argument('headless')               # headless 모드, 웹 
    #options.add_arguemnt('User-Agent: ...')        # 브라우져 유일 값, headless모드 일 때 서버가 체크 할 경우를 대비
    #options.add_arguemnt('lang=ko_KR')             # 브라우져 언어를 설정, headless모드 일 때 서버가 언어 체크 할 경우를 대비
    #options.add_argument('window-size=1920x1080')  # 브라우져 해상도(fullhd 기준으로 해상도 조절)
    options.add_experimental_option('excludeSwitches', ['enable-logging']) # bluetooth_adapter 사용 안함
    options.add_argument("disable-gpu")             # 그래픽카드 가속모드 사용 안함(처리 속도 향상)

    driver = webdriver.Chrome(chromedriver_pathchrome_options=options)

    driver.implicitly_wait(10)          # 브라우져 실행 대기
    driver.get('https://www.google.com/')



options.add_experimental_option('excludeSwitches', ['enable-logging']) 같은 경우 지정 하지 않으면 아래와 같은 에러가 출력 된다. 프로그램 구동에는 지장 없다. 
DevTools listening on ws://.../devtools/browser
/806b7a98-a6f7-4f2f-a816-1cbc6bd4c740
[8136:9540:0630/180235.500:ERROR:device_event_log_impl.cc(214)] [18:02:35.499] 
Bluetooth: bluetooth_adapter_winrt.cc:1072 Getting Default Adapter failed.

options.add_argument('headless') 옵션으로 진행할 경우 크롬 브라우져를 안보이면서 실행 된다. 이 때 접속 해야할 사이트에서 클라이언트가 headless인지 확인하기 위해 options.add_arguemnt('User-Agent: ...')를 확인하여 매크로인지 여부를 확인한다고 한다. 그래서 해당 옵션을 아래와 같이 지정하여 정상적인 브라우져로 인식 하게 한다


options.add_arguemnt('User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36')

options.add_argument('headless')이 속도가 조금 빠르다고 하나. 중요한건 서버의 응답 속도이므로 개발과정에서 배제하는 게 좋다.


빌드 & 배포

빌드

정상적으로 빌드가 되었을 때 

    PS C:\...\python_selenium_automation> pyinstaller .\src\automation.py -F --add-binary ".\91\chromedriver.exe;.\91\"
    46 INFO: PyInstaller: 4.3
    46 INFO: Python: 3.9.5
    72 INFO: Platform: Windows-10-10.0.19042-SP0
    ...
    10038 INFO: Building EXE from EXE-00.toc completed successfully.

-F는 하나의 파일로 만드는 옵션이다.
실행 파일은 .\dist 경로에 생성되는데 이 옵션이 없는 경유 여러가지 파일들이 같이 생성 되어 있어 배포나 실행에 불편함이 있다, 단 실행시 임시폴더를 생성하여 관련 파일들을 배치 시킴으로 실생 시간이 없을 때 보다 조금 더 걸린다.

--add-binary는 위에서 임포트한(from selenium import webdriver)에 대한 모듈을 실행파일에 포함하기 위함이다.
--add-binary 없이 빌드 하였을 경우 빌드는 잘 되나 아래와 같은 에러가 발생한다.

    C:\...\dist\automation.exe
    Traceback (most recent call last):
      File "automation.py", line 25, in <module>
    ModuleNotFoundError: No module named 'selenium'
    [15836] Failed to execute script sample

이건 chromedriver을 찾지 못 해 발생한 문제이다. 하여 해당 바이너리를 포함해 빌드 한다.

정상적으로 빌드가 되었다면 .\dist\automation.exe 파일이 생성 되었을 것이다.


    C:.
    ├─.vscode
    │  └─.ropeproject
    ├─91
    ├─build
    │  └─automation
    ├─dist
    ├─python_selenium_automation
    └─src
        └─__pycache__


터미널에서 실행파일 실행

임시폴더를 생성하고 그 안에 chromedriver.exe 생성

    PS C:\...\python_selenium_automation\dist> .\automation.exe
    find chromedriver
     Absolute path to chromedriver: C:\...\Temp\_MEI51922\.\91\chromedriver.exe



cmd 창에서 실생

파일 경로 창에 'cmd'를 입력하고 엔터 치면 해당 경로로 cmd창이 열린다.

실행파일 실행을 입력하고 엔터친다.
    
    PS C:\...\python_selenium_automation\dist> .\automation.exe

파이썬 경로를 확인 할 수 없을 때 발생 되는 오류

pyinstaller : 'pyinstaller' 용어가 cmdlet, 함수, 스크립트 파일 또는 실행할 수 있는 프로그램 이름으로 인식되지 않습니다. 이름이 정확한지 확인하고 경로가 포함된 경우 경로가 올바른지 검증 한 다음 다시 시도하십시오.

  • VSCode에서 경로 확인

  • 윈도우 경로 확인



댓글

이 블로그의 인기 게시물

[유니티] 오류 사례 "Moving file failed", "Temp"

[Flutter] flutter setup error, "Unable to locate Android SDK.", "[!] Connected device"

[유니티] 구글 지도 출력 샘플