Untitled

mail@pastecode.io avatar
unknown
python
a year ago
4.1 kB
3
Indexable
Never
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import webbrowser
from tkinter import *
from datetime import date
from time import sleep


def callback(url):
    webbrowser.open_new(url)

def wait_until(browser, xpath_str):
    WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.XPATH, xpath_str)))
    return 

def Do(start_yy, start_mm, start_dd, arrive_yy, arrive_mm, arrive_dd, country:str, airport:str): # 함수 인풋으로 넣고 하자 
    global entry5, entry6, entry7

    try:
        # if(flag):
        options = webdriver.ChromeOptions()
        options.add_argument("headless")
        # browser = webdriver.Chrome(options=options)
        browser = webdriver.Chrome()
        # else:
        url = "https://flight.naver.com/"
        browser.get(url=url)
        # print(1)
        wait_until(browser, '//*[@id="__next"]/div/div[1]/div[9]/div/div[2]/button[2]')
        end = browser.find_element(By.XPATH, '//*[@id="__next"]/div/div[1]/div[9]/div/div[2]/button[2]')
        end.click()


        # print(2)
        # 해당 달 -> 해당 일 
        wait_until(browser, '//button[text() = "가는 날"]')
        begin_date = browser.find_element(By.XPATH, '//button[text() = "가는 날"]')
        begin_date.click()
        # print(3)


        # how to 
        sleep(1)
        start_day = '\"' + str(start_dd) + '\"'
        wait_until(browser, '//b[text() = '+start_day+']')
        start = browser.find_elements(By.XPATH,  '//b[text() = '+start_day+']')
        start[start_mm-m].click() # 월 
        # print(4)
        sleep(1)
        # relative month distance 
        # from this month, calculate 
        #              0 -> this month + 1 

        ## caution : start < end 
        ## how to check ?
        # day = 날짜 
        arr_day = '\"' + str(arrive_dd) + '\"'
        wait_until(browser, '//b[text() = '+arr_day+']')
        arr = browser.find_elements(By.XPATH,  '//b[text() = '+arr_day+']')
        arr[arrive_mm-m].click() # 1 : 0, 2 : 1, 3: 2, 4: 3


        # print(5)
        # airport 

        wait_until(browser, '//i[text() = "선택"]')
        where = browser.find_element(By.XPATH, '//i[text() = "선택"]')
        where.click()

        # input1 = input("국가 ? : ")
        country = '\"' + country + '\"'

        wait_until(browser, '//button[text() = ' + country + ']')
        to = browser.find_element(By.XPATH, '//button[text() = ' + country + ']')
        to.click()
        # airport.click()

        # 국내 []
        airport = '\"' + airport + '\"' # input()
        wait_until(browser, '//i[text() = ' + airport + ']')
        city = browser.find_element(By.XPATH, '//i[text() = ' + airport + ']')
        city.click()

        print("WHY 3")
        wait_until(browser, '//span[text() = "항공권 검색"]')
        search = browser.find_element(By.XPATH, '//span[text() = "항공권 검색"]')
        search.click()
        sleep(15)
        elem = WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.XPATH,'//div[@class="concurrent_ConcurrentItemContainer__2lQVG result"]')))
        output = elem.text
        output = output.split('\n')
        # print(output)
        
        print("WHY 3")
        # coming 
        entry5.delete(0,"end")
        entry5.insert(0, text = " ".join(output[:5]))
        entry6.delete(0,"end")
        entry6.insert(0,text = " ".join(output[5:10]))
        entry7.delete(0,"end")
        entry7.index(0, text = " ".join(output[10:]))

        # url = Button(info, text = "naver flight", fg = "blue", cursor="hand2")
        url.pack()
        url.bind("<Button-1>", lambda e: callback(browser.current_url))
        # callback(browser.current_url)
        # link1 = Label(root, text="Google Hyperlink", fg="blue", cursor="hand2")
        # input("종료하려면 a를 입력해주세요 ")
        return 0
    except:
        # browser.quit()
        return 1