[python] 파이썬 pyautogui를 사용하여 특정 웹 페이지 업로드하기

파이썬의 pyautogui 라이브러리는 시스템 자동화를 위해 마우스 및 키보드 동작을 제어하는 데 사용됩니다. 이 글에서는 pyautogui를 사용하여 특정 웹 페이지에 파일을 업로드하는 방법을 알아보겠습니다.

1. 필요한 패키지 설치하기

pyautogui를 사용하기 위해 먼저 필요한 패키지를 설치해야 합니다. 아래 명령을 사용하여 pyautogui를 설치합니다.

pip install pyautogui

2. 웹 페이지 업로드 스크립트 작성하기

import pyautogui
import time

# 업로드할 파일 경로
file_path = "C:/example/file.txt"

# 파일 업로드 버튼의 좌표
upload_button_x = 100
upload_button_y = 200

# 파일 경로 입력 창(예: input[type=file])의 좌표
file_input_x = 300
file_input_y = 250

# 파일 올리기 버튼의 좌표
submit_button_x = 400
submit_button_y = 300

# 파일 업로드하기
def upload_file(file_path):
    # 파일 업로드 버튼 클릭
    pyautogui.click(upload_button_x, upload_button_y)

    # 파일 경로 입력 창 클릭
    pyautogui.click(file_input_x, file_input_y)
    time.sleep(1)

    # 파일 경로 입력
    pyautogui.write(file_path)
    time.sleep(1)

    # 파일 업로드
    pyautogui.press('enter')
    time.sleep(1)

    # 파일 올리기 버튼 클릭
    pyautogui.click(submit_button_x, submit_button_y)
    time.sleep(1)

# 파일 업로드 함수 호출
upload_file(file_path)

위의 예제 코드에서는 pyautogui를 사용하여 파일 업로드 스크립트를 작성했습니다. 파일 업로드 버튼, 파일 경로 입력 창, 파일 올리기 버튼의 좌표를 설정하고, pyautogui의 함수를 사용하여 해당 좌표를 클릭하고 입력하며 업로드합니다.

3. 주의사항

참고 자료