110 lines
4.4 KiB
Python
110 lines
4.4 KiB
Python
import os
|
|
import io
|
|
import time
|
|
import shutil
|
|
from pywinauto import Application, findwindows
|
|
from contextlib import redirect_stdout
|
|
|
|
def clear_directory(directory_path):
|
|
"""디렉토리 내 모든 파일을 삭제"""
|
|
if os.path.exists(directory_path):
|
|
shutil.rmtree(directory_path) # 폴더 삭제 후 재생성
|
|
os.makedirs(directory_path)
|
|
|
|
def start_whale_in_secret_mode():
|
|
# 실행 경로 설정
|
|
whale_exe_path = os.path.join(os.getcwd(), "browsers", "whale", "whale.exe")
|
|
user_data_dir = os.path.join(os.getcwd(), "browsers", "whale", "user_data")
|
|
cache_dir = os.path.join(os.getcwd(), "browsers", "whale", "cache")
|
|
|
|
# # 유저 데이터 및 캐시 폴더 비우기
|
|
# clear_directory(user_data_dir)
|
|
# clear_directory(cache_dir)
|
|
|
|
# 웨일을 시크릿 모드로 시작
|
|
app = Application(backend="uia").start(
|
|
f'"{whale_exe_path}" --incognito --user-data-dir="{user_data_dir}" --disk-cache-dir="{cache_dir}"'
|
|
)
|
|
time.sleep(1) # 웨일이 완전히 로드될 때까지 대기
|
|
return app
|
|
|
|
def find_whale_window(app):
|
|
# '새 시크릿 탭 - Whale' 창을 찾기 위해 PID 검색
|
|
whale_pid = None
|
|
windows = findwindows.find_elements()
|
|
for window in windows:
|
|
if window.name == '새 시크릿 탭 - Whale':
|
|
whale_pid = window.process_id
|
|
break
|
|
|
|
# PID가 있으면 해당 창을 연결
|
|
if whale_pid:
|
|
app = Application(backend="uia").connect(process=whale_pid)
|
|
whale_window = app.top_window()
|
|
whale_window.set_focus()
|
|
return whale_window
|
|
else:
|
|
print("'새 시크릿 탭 - Whale' 창을 찾을 수 없습니다.")
|
|
return None
|
|
|
|
def navigate_to_url(whale_window, url):
|
|
# 주소창을 찾고 URL을 입력 후 이동
|
|
try:
|
|
address_bar = whale_window.child_window(title="주소창 및 검색창", control_type="Edit")
|
|
address_bar.click_input()
|
|
address_bar.type_keys(f"{url}{{ENTER}}", with_spaces=True)
|
|
time.sleep(3) # 페이지 로딩 대기
|
|
except Exception as e:
|
|
print(f"주소창에 접근할 수 없습니다: {e}")
|
|
|
|
def right_click_on_image_and_inspect(whale_window, output_file):
|
|
# 창의 중앙 좌표 계산 및 마우스를 이동 후 우클릭하여 컨텍스트 메뉴 열기
|
|
try:
|
|
rect = whale_window.rectangle()
|
|
center_x = (rect.left + rect.right) // 2
|
|
center_y = (rect.top + rect.bottom) // 2
|
|
|
|
# 창 중앙에서 우클릭
|
|
whale_window.right_click_input(coords=(center_x, center_y))
|
|
time.sleep(1)
|
|
|
|
# '이미지 번역 (R)' 메뉴 항목 클릭
|
|
translate_menu_item = whale_window.child_window(title="이미지 번역 (R)", control_type="MenuItem")
|
|
translate_menu_item.click_input()
|
|
print("이미지 번역 명령이 실행되었습니다.")
|
|
|
|
# 0.2초 간격으로 2초 동안 제어 가능한 요소를 수집하고 로그 파일에 저장
|
|
control_info = ""
|
|
for i in range(10): # 2초 동안 0.2초 간격
|
|
control_info += f"\n=== {round((i + 1) * 0.2, 1)}초 후 컨텍스트 메뉴 제어 가능한 요소 ===\n"
|
|
try:
|
|
# StringIO 객체에 출력 캡처
|
|
output = io.StringIO()
|
|
with redirect_stdout(output):
|
|
whale_window.print_control_identifiers()
|
|
control_info += output.getvalue()
|
|
except Exception as e:
|
|
control_info += f"오류 발생: {e}\n"
|
|
time.sleep(0.2)
|
|
|
|
# 수집한 정보를 파일에 기록
|
|
with open(output_file, "w", encoding="utf-8") as f:
|
|
f.write(control_info)
|
|
|
|
print(f"컨트롤 정보가 {output_file}에 저장되었습니다.")
|
|
|
|
except Exception as e:
|
|
print(f"컨텍스트 메뉴를 열거나 요소를 가져오는 중 오류 발생: {e}")
|
|
|
|
# 실행 순서
|
|
if __name__ == "__main__":
|
|
app = start_whale_in_secret_mode()
|
|
whale_window = find_whale_window(app)
|
|
|
|
if whale_window:
|
|
url = "https://file.percenty.co.kr/public/652bed8e865b1f32ea62bf1f/products/66f37db773994c46d385c3ec/f24f75e8-7601-4e3f-9f1d-4f2edaf08155.jpg"
|
|
# url = 'https://img.alicdn.com/imgextra/i3/2206707238989/O1CN01q5dVHX2GH1EgiFpIR_!!2206707238989.jpg_Q75.jpg'
|
|
navigate_to_url(whale_window, url)
|
|
output_file_path = os.path.join(os.getcwd(), "whale_context_menu_output.txt")
|
|
right_click_on_image_and_inspect(whale_window, output_file_path)
|