Computer Security

#4 Selenium을 이용한 매크로(자동배정) 본문

프로그래밍/Python 웹 스크래핑

#4 Selenium을 이용한 매크로(자동배정)

쿠리 Kuri 2022. 7. 1. 18:30

악용할 예시가 있기 때문에

코드 몇줄은 넣지 않고 올린다.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import schedule
import time
import datetime
import sys 
import datetime as dt




driver = webdriver.Chrome('./chromedriver.exe')
# 사이즈조절
driver.set_window_size(1920, 1080)  # (가로, 세로)
driver.get('site') # 페이지 이동
print(driver.window_handles)

driver.switch_to.frame(driver.find_element(By.XPATH, "//div[@class='leftLoginBox']/iframe[@title='login']"))

userId.send_keys('ID') # 로그인 할 계정 id

userPwd.send_keys('PW') # 로그인 할 계정의 패스워드
userPwd.send_keys(Keys.ENTER)

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


driver.get('예매')
#ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
#예매 창 


endhope=False
while not endhope:
    tim=dt.datetime.now()
    if tim.second>=59 and tim.microsecond>800000:
        driver.refresh()
        endhope=True
        print(tim)
    else:
        time.sleep(0.1)
        print(tim)


 #정해진 시간에 새로 고침

wait_until('//*[@id="div_checkDontsee_PT002_46_1"]/div[2]/button')

pop.click()

wait_until('/html/body/div[6]/div[4]/div[4]/div[2]/div[5]')


# 예매하기 눌러서 새창이 뜨면 포커스를 새창으로 변경
print(driver.window_handles)
driver.switch_to.window(driver.window_handles[1])
print(driver.window_handles)

#좌석 일반석 선택
wait_until('/html/body/div[1]/div[3]/div[2]/div[1]/a')
seat = driver.find_element(By.XPATH, '/html/body/div[1]/div[3]/div[2]/div[1]/a')
seat.click()

#자동배정 클릭
wait_until('/html/body/div[1]/div[3]/div[2]/div[3]/a[1]/img')
click1 = driver.find_element(By.XPATH,'/html/body/div[1]/div[3]/div[2]/div[3]/a[1]/img')
click1.click()

내가 주목할 코드는 while 문과 if 문을 통해

현재 시간을 계속 받아오면서 내가 원하는 시간까지 무한루프를 돌다가 빠져나오는 코드이다.

 

endhope=False
while not endhope:
    tim=dt.datetime.now()
    if tim.second>=59 and tim.microsecond>800000:
        driver.refresh()
        endhope=True
        print(tim)
    else:
        time.sleep(0.1)
        print(tim)

셀레니움을 통해 59.8초에 정확히 새로고침되는 매크로 이다.

중요 코드 몇줄은 삭제했으니..ㅎㅎ 괜찮겠지

Comments