-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.py
202 lines (159 loc) · 6.78 KB
/
function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
from playwright.sync_api import sync_playwright
import time
import os
from utils import handle_dialog, login_and_cache, find_file
# 카드 정보 오류 확인
class PaymentVerificationError(Exception):
pass
# 로그인 캐시 파일 경로
AUTH_FILE = "auth_state.json"
def execute_drive(card_info):
with sync_playwright() as p:
# 브라우저 열기
browser = p.chromium.launch(headless=False)
# 캐시 파일이 존재하면 로그인
if os.path.exists(AUTH_FILE):
print("이전 로그인 정보로 로그인을 시도합니다.")
context = browser.new_context(storage_state=AUTH_FILE)
# 캐시 파일이 존재하지 않으면 수동으로 로그인
else:
print(
"이전 로그인 정보를 찾지 못했습니다. 수동으로 로그인해주세요. 20초 후에 자동화 프로그램이 시작됩니다."
)
context = browser.new_context()
page = context.new_page()
# 캐시로 웹사이트 접속하기
try:
page.goto(
"https://service.epost.go.kr/front.commonpostplus.RetrieveAcceptPlus.postal?gubun=1"
)
# 로그인 성공했는지 확인
member_info_visible = page.is_visible("div.memberInfo")
if not member_info_visible:
raise Exception("로그인이 필요합니다.")
except Exception as e:
print(
f"로그인이 실패했습니다. 수동으로 로그인해주세요. 20초 후에 자동화 프로그램이 시작됩니다.: {str(e)}"
)
login_and_cache(context, page, AUTH_FILE)
# 로그인 끝나면 다시 페이지로 이동
page.goto(
"https://service.epost.go.kr/front.commonpostplus.RetrieveAcceptPlus.postal?gubun=1"
)
# 보내는분 정보 수정 (이름 입력)
page.fill("#tSndNm", "")
page.fill("#tSndNm", "블랙독")
# 배송 정보 수신 (SMS)
page.click("#recevYn4")
# 팝업 창 popup_info로 받아주기
with page.expect_popup() as popup_info:
page.click("#tSpecialDivCd4") # '준등기' 버튼 클릭하기
# 팝업창 핸들링 (닫기)
popup = popup_info.value
popup.close()
# 우체통 접수 서비스
# 팝업 창 popup_info로 받아주기
with page.expect_popup() as popup_info:
page.click("#mBoxThwrY2") # 우체통 접수 신청하기
# 팝업창 핸들링 (닫기)
popup = popup_info.value
popup.close()
# 내용물 코드 '510' 선택
page.select_option("#semiCode", "510")
# 업로드할 주소 파일 찾기 (1개만!)
file_to_upload = find_file()
# 주소(파일)이용하기
with page.expect_popup() as popup_info:
page.click("#btnUseAddrFile")
popup = popup_info.value
popup.set_input_files("#uploadFile", file_to_upload)
# 카드 번호 입력
card1 = card_info["card1"]
card2 = card_info["card2"]
card3 = card_info["card3"]
card4 = card_info["card4"]
month = card_info["month"]
year = card_info["year"]
pass1 = card_info["pass1"]
pass2 = card_info["pass2"]
credit_birth = card_info["credit_birth"]
# 카드 정보 입력
credit_no_input1 = page.locator("#creditNo1")
credit_no_input1.scroll_into_view_if_needed()
credit_no_input1.fill(card1)
credit_no_input1 = page.locator("#creditNo2")
credit_no_input1.scroll_into_view_if_needed()
credit_no_input1.fill(card2)
credit_no_input1 = page.locator("#creditNo3")
credit_no_input1.scroll_into_view_if_needed()
credit_no_input1.fill(card3)
credit_no_input1 = page.locator("#creditNo4")
credit_no_input1.scroll_into_view_if_needed()
credit_no_input1.fill(card4)
creditExp1 = page.locator("#creditExp1")
creditExp2 = page.locator("#creditExp2")
creditExp1.fill(month)
creditExp2.fill(year)
creditPwd1 = page.locator("#creditPwd1")
creditPwd2 = page.locator("#creditPwd2")
creditPwd1.fill(pass1)
creditPwd2.fill(pass2)
creditBirth = page.locator("#creditBirth1")
creditBirth.fill(credit_birth)
# 결제카드검증
page.wait_for_selector("#certCreditInfo") # Wait for the button to appear
time.sleep(1)
page.click("#certCreditInfo")
time.sleep(1)
page.click("#certCreditInfo")
error_locator1 = page.locator("li#mag4.red_b")
error_locator2 = page.locator("li#mag5.red_b")
error_locator3 = page.locator("li#mag6.red_b")
if error_locator1.is_visible():
raise PaymentVerificationError(
"Error: 결제카드 검증에 실패했습니다. 결제카드정보를 확인해주세요."
)
if error_locator2.is_visible():
raise PaymentVerificationError(
"Error: 현재 검증서비스를 이용할 수 없습니다."
)
if error_locator3.is_visible():
raise PaymentVerificationError(
"Error: 결제카드 검증에 실패했습니다. 네트워크 상태를 확인해주세요."
)
# 개인정보 수집 동의
# 스크롤 끝까지 내리고 '동의합니다' 체크
page.wait_for_selector("#agreeBox") # Wait until the element is available
page.evaluate(
"""() => {
var element = document.getElementById('agreeBox');
element.scrollIntoView(); // Scroll the box into view
element.scrollTop = element.scrollHeight; // Scroll inside the box to the bottom
}"""
)
page.click("#applyAgree_Priv")
# 접수 신청
page.on("dialog", handle_dialog)
page.click("#btnReqClick")
# 주소 검증 팝업 (자동 처리)
# 다시 접수 신청
page.click("#btnReqClick")
page.click("#btnReqClick")
# Wait for the notification to appear
page.wait_for_selector("#notiContentDiv.show")
# 접수 신청하면 새로운 팝업이 열림
confirm_button = page.wait_for_selector(
"a.btn_m.bg_red#confirmBtn", state="visible"
)
# 최종 확인 버튼 누르기
confirm_button.click()
# 페이지가 넘어가면 접수 신청 완료
if (
page.url
== "https://service.epost.go.kr/front.commonpostplus.RetrieveRegiPostPlus.postal"
):
print("간편사전접수 신청이 완료되었습니다.")
# 대기
time.sleep(1000000)
# page.close()
# browser.close()