JUST DO IT!

[TIL]KDT_20230420 ๋ณธ๋ฌธ

TIL

[TIL]KDT_20230420

sunhokimDev 2023. 4. 20. 17:01

๐Ÿ“š KDT WEEK 3 DAY 4 TIL

  • Selenium
    • ์š”์†Œ ์ฐพ๊ธฐ
    • wait and call

 


์‹œ์ž‘ํ•˜๊ธฐ ์ „์—, ์ด ๊ณผ์ •์—๋Š” ๋‘ ๊ฐœ์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

 

 

์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, ํ•ญ์ƒ --upgrade๋ฅผ ํ†ตํ•ด ์ตœ์‹ ๋ฒ„์ „์ด ๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž!

 


 

๐ŸŸฅ Selenium

Python์„ ์ด์šฉํ•ด์„œ ์›น ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋™ํ™” ํ”„๋ ˆ์ž„์›Œํฌ

# Selenium๊ณผ ํฌ๋กฌ ์›น ๋“œ๋ผ์ด๋ฒ„๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

#ํŒŒ์ด์ฌ์—์„œ ํฌ๋กฌ์„ ์‹คํ–‰์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

#์‹คํ–‰์‹œํ‚จ ํฌ๋กฌ์—์„œ ํ•ด๋‹น ์‚ฌ์ดํŠธ๋กœ ์ด๋™(Request)
driver.get("https://www.example.com")

print(driver.page_source)

 

page_source๋ฅผ ์ฐ์–ด๋‚ธ ๊ฒฐ๊ณผ(์ผ๋ถ€), html์ด ๋„˜์–ด์˜จ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ ์‹คํ–‰ํ•ด๋ณด๋ฉด, ์ž๋™์œผ๋กœ ํฌ๋กฌ ์ฐฝ์ด ์—ด๋ฆฌ๊ณ  ์ž…๋ ฅํ•œ URL(example.net)์— ์ ‘์†ํ•˜๊ฒŒ ๋œ๋‹ค.

ํ•˜์ง€๋งŒ ์‹คํ–‰์ด ๋๋‚œ ๋’ค์—๋„ ํฌ๋กฌ ์ฐฝ์ด ๊บผ์ง€์ง€ ์•Š๊ณ  ์œ ์ง€๋˜๋Š”๊ฑธ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

์ผ์ •ํ•œ ์ผ์„ ์ˆ˜ํ–‰ํ•œ ๋’ค ์ž๋™์œผ๋กœ ์‹คํ–‰์„ ๋„๋Š” ์ฝ”๋“œ..

ํŒŒ์ด์ฌ์—๋Š” ๋น„์Šทํ•œ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ๊ฐ•๋ ฅํ•œ ๊ตฌ๋ฌธ์ด ์žˆ๋‹ค.

 

with..as ๊ตฌ๋ฌธ์„ ํ™œ์šฉํ•œ๋‹ค!

with as ์•„๋ž˜๋กœ ์‹คํ–‰ํ•˜๊ณ ์ž ํ•˜๋Š” ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•œ๋‹ค.

 

์—ฌ๊ธฐ์„œ ํ•œ ๊ฐ€์ง€๋ฅผ ๋” ์ถ”๊ฐ€ํ•ด์„œ ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•ด๋ณด๊ฒ ๋‹ค.

์–ด์ œ bs4์—์„œ ์š”์†Œ๋ฅผ ์ฐพ๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ๋˜๊ฒƒ์ฒ˜๋Ÿผ, selenium์—๋„ ์ด ๊ธฐ๋Šฅ์ด ์กด์žฌํ•œ๋‹ค.

 

.find_element(by, target)

  • by : ๋Œ€์ƒ์„ ์ฐพ๋Š” ๊ธฐ์ค€ : ID, TAG_NAME, CLASS_NAME ...
  • target : ๋Œ€์ƒ์˜ ์†์„ฑ

์—ฌ๋Ÿฌ ์š”์†Œ๋ฅผ ์ฐพ๊ณ  ์‹ถ์„ ๋•Œ๋Š”, .find_elements(by, target)์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

from selenium.webdriver.common.by import By

#with..as ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ print(x)๊นŒ์ง€ ๋™์ž‘์ด ๋๋‚˜๋ฉด, ์ž๋™์œผ๋กœ ํฌ๋กฌ์€ ์ข…๋ฃŒ๋œ๋‹ค.
with webdriver.Chrome(service=Service(ChromeDriverManager().install())) as driver:
    driver.get("http://www.example.net")
    
    # p ํƒœ๊ทธ ์š”์†Œ๋“ค์„ ๋ชจ๋‘ ๊ฐ€์ ธ์™€์„œ ํ•ด๋‹น ์ฝ˜ํ…์ธ ๋ฅผ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.
    pList = [x.text for x in driver.find_elements(By.TAG_NAME, "p")]
    for x in pList:
    	print(x)

์œ„ ์ฝ”๋“œ์˜ ๊ฒฐ๊ณผ. ๋งŽ์„์ค„ ์•Œ์•˜๋Š”๋ฐ, ๋‘ ๊ฐ€์ง€๋ฐ–์— ์—†์—ˆ๋‹ค.

 


 

XPath

์Šคํฌ๋ž˜ํ•‘์— ํ™œ์šฉํ•œ ์‚ฌ์ดํŠธ : https://indistreet.com/live?sortOption=startDate%3AASC 

 

ํ•ญ์ƒํ•˜๋˜ ๊ฒƒ์ฒ˜๋Ÿผ, ํ•„์š”ํ•œ ์š”์†Œ๊ฐ€ ์–ด๋””์žˆ๋Š”์ง€ ์ฐพ์•„๋ณด์ž..

 

์–ด๋–ค ๊ณต์—ฐ์˜ ์ œ๋ชฉ์˜ ์œ„์น˜๋ฅผ ๋ดค๋”๋‹ˆ, ํด๋ž˜์Šค ์ด๋ฆ„์ด ํŠน์ดํ•˜๋‹ค.

 

์š”์ฆ˜ ์›น์‚ฌ์ดํŠธ์˜ ํŠธ๋ Œ๋“œ๊ฐ€ ์‰ฌ์šด ์Šคํฌ๋ž˜ํ•‘์„ ๋ง‰๊ธฐ ์œ„ํ•ด ์ด๋Ÿฌํ•œ ํด๋ž˜์Šค ์ด๋ฆ„์„ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•œ๋‹ค..

๋”ฐ๋ผ์„œ ํด๋ž˜์Šค์˜ ์ด๋ฆ„์ด ์•„๋‹Œ, XPath๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

ํ•ด๋‹น ํƒœ๊ทธ๋ฅผ ์˜ค๋ฅธ์ชฝ ๋งˆ์šฐ์Šค ํด๋ฆญํ•˜๊ณ , Copy XPath๋ฅผ ํ•˜๋ฉด..

 

๊ฐ€ ๋ณต์‚ฌ๋ฌ์Œ์„ ์•Œ ์ˆ˜์žˆ๋‹ค.

 

์ด XPath๋ฅผ find_element ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์ ธ์˜ค๋ ค๊ณ  ํ•˜๋ฉด..

์ด์ฒ˜๋Ÿผ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค.

 

์ด์œ ๋Š” ์š”์ฒญ์„ ๋ณด๋‚ธ ์‚ฌ์ดํŠธ๊ฐ€ ๋™์  ์›น ์‚ฌ์ดํŠธ์ด๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์ดํŠธ๊ฐ€ ์˜จ์ „ํžˆ ๋กœ๋”ฉํ•˜๋Š” ๋ฐ์— ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

์ด๋•Œ selenium์˜ wait๋ฅผ ํ™œ์šฉํ•œ๋‹ค.

 

 


 

Implicit Wait

.implicitly_wait(์‹œ๊ฐ„) : '์ดˆ' ๋‹จ์œ„์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ํ•ด๋‹น ์‹œ๊ฐ„๋งŒํผ ๊ธฐ๋‹ค๋ฆฌ๋„๋ก ํ•œ๋‹ค.

๋‹จ, ๊ทธ ์‹œ๊ฐ„์„ ๋ชจ๋‘ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋กœ๋”ฉ์ด ๋˜๋ฉด ๋„˜์–ด๊ฐ„๋‹ค!

 

implicit wait์„ ํ™œ์šฉํ–ˆ๋”๋‹ˆ, ๋‚ด๊ฐ€ ์›ํ–ˆ๋˜ ๋‚ด์šฉ์ด ์ •์ƒ์ ์œผ๋กœ ์ถœ๋ ฅ๋˜์—ˆ๋‹ค.

 

Explicit Wait

์ด๊ฑด ์ฝ”๋“œ๋ฅผ ๋จผ์ € ๋ณด๊ณ  ์„ค๋ช…ํ•˜๊ฒ ๋‹ค.

#Explicit wait์— ์‚ฌ์šฉ๋จ
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

with webdriver.Chrome(service= Service(ChromeDriverManager().install())) as driver:
    driver.get("https://indistreet.com/live?sortOption=startDate%3AASC")
    
    #Explicit wait
    element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="__next"]/div/main/div[2]/div/div[4]/div[1]/div[1]/div/a/div[2]/p[1]')))

    print(element.text)

๋จผ์ €, Explicit Wait์€ WebDriverWait() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ด๋ฃจ์–ด์ง„๋‹ค.

WebDriverWait(driver, 10)์€ implicit wait์ฒ˜๋Ÿผ 10์ดˆ์˜ ํ•œ๊ณ„ ์‹œ๊ฐ„์„ ๋‘๋Š” ๊ฒƒ์ด๋‹ค.

๊ทธ๋ฆฌ๊ณ  Explicit Wait์˜ ์—ญํ• ์„ ํ•ด์ค„ ๋‘ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

  • until() : ์–ด๋– ํ•œ ์ธ์ž์˜ ์กฐ๊ฑด์ด ๋งŒ์กฑ๋  ๋•Œ๊นŒ์ง€
  • until_not() : ์–ด๋– ํ•œ ์ธ์ž์˜ ์กฐ๊ฑด์ด ๋งŒ์กฑ๋˜์ง€ ์•Š์„ ๋•Œ๊นŒ์ง€

์‚ฌ์šฉ ์˜ˆ๋ฅผ ๋ณด๋ฉด,

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "target")))

์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

 

์—ฌ๊ธฐ์„œ EC๋Š” expected_conditions๋กœ, selenium์—์„œ ์ •์˜๋œ ์กฐ๊ฑด์ด๋‹ค.

"~๊ฐ€ ์กด์žฌํ•˜๋ฉด" ์œผ๋กœ ์ดํ•ดํ•˜๋ฉด ๋œ๋‹ค๊ณ  ํ•œ๋‹ค.

 

๋”ฐ๋ผ์„œ, ์œ„ ์ฝ”๋“œ๋Š” "target์ด๋ž€ ID๊ฐ€ ๋กœ๋”ฉ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ค๋ผ. ๋‹จ 10์ดˆ๊นŒ์ง€๋งŒ" ์œผ๋กœ ์ดํ•ดํ•˜๋ฉด ๋œ๋‹ค!

 

๊ทธ๋ž˜์„œ.. ์•„๊นŒ implicit wait์˜ ์ฝ”๋“œ๋ฅผ explicit wait์œผ๋กœ ๊ณ ์ณ๋ณด๋ฉด..

from selenium.webdriver.support import expected_conditions as EC

with webdriver.Chrome(service= Service(ChromeDriverManager().install())) as driver:
    driver.get("https://indistreet.com/live?sortOption=startDate%3AASC")
    
    #explicit wait
    element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="__next"]/div/main/div[2]/div/div[4]/div[1]/div[1]/div/a/div[2]/p[1]')))

    print(element.text)

์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋ฉด ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.

 

ํ•˜๋‚˜์˜ ๊ณต์—ฐ ์ œ๋ชฉ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ œ๋ชฉ์„ ๋ฝ‘์œผ๋ ค๋ฉด ์–ด๋–กํ• ๊นŒ?

 

๋‹ค๋ฅธ ์ œ๋ชฉ๋“ค์˜ XPath๋ฅผ ์•Œ์•„๋ณด๋ฉด ๊ฒฐ๊ณผ๋Š” ์‰ฝ๋‹ค.

๋ช‡ ๊ฐœ๋ฅผ ๊ธ์–ด๋ณด๋ฉด, ์–ด๋– ํ•œ ๋ถ€๋ถ„์—์„œ ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

์ด์ „์— ํ•ด๋ดค๋˜ ๊ฒƒ์ฒ˜๋Ÿผ, for๋ฌธ์„ ํ™œ์šฉํ•˜๋ฉด ์—ฌ๋Ÿฌ ์ œ๋ชฉ๋“ค์„ ๋ฝ‘์•„๋‚ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค!

 


๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌํ•˜๊ธฐ

๋„ค์ด๋ฒ„์—์„œ ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ์ฐพ์•„ class ์ด๋ฆ„์„ ์ฐพ๊ณ , ๊ทธ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค.

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.naver.com/")
driver.implicitly_wait(5)

button  = driver.find_element(By.CLASS_NAME, "link_login")
ActionChains(driver).click(button).perform() # ๊ทธ ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ๋ผ

๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์ค„์ด ๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋‚ด์šฉ์ด๋‹ค.

ActionChains ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์–ด๋””์˜ ์–ด๋–ค ๋ฒ„ํŠผ์„ ์–ด๋–ป๊ฒŒํ• ์ง€ ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋”๋ธ”ํด๋ฆญ, ํ™€๋“œ ๋“ฑ ๋” ๋งŽ์€ ๊ธฐ๋Šฅ์€ https://www.selenium.dev/documentation/webdriver/actions_api/mouse/ ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

 

ํ‚ค๋ณด๋“œ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌํ•˜๊ธฐ

๋„ค์ด๋ฒ„์—์„œ ์ž๋™์œผ๋กœ ๋กœ๊ทธ์ธ ํ™”๋ฉด์œผ๋กœ ๋„˜์–ด๊ฐ€ ์•„์ด๋””์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ๋กœ๊ทธ์ธ์„ ์‹œ๋„ํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค.

from selenium import webdriver
from selenium.webdriver import ActionChains
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver import Keys, ActionChains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.naver.com/")
driver.implicitly_wait(3)

# ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ ๋„˜์–ด๊ฐ€๊ธฐ
button = driver.find_element(By.CLASS_NAME, "link_login")
ActionChains(driver).click(button).perform()
time.sleep(1)

# ์•„์ด๋”” ์ž…๋ ฅ
id_input = driver.find_element(By.ID, "id")
ActionChains(driver).send_keys_to_element(id_input, "idid1234").perform()
time.sleep(1)

# ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ
pw_input = driver.find_element(By.ID, "pw")
ActionChains(driver).send_keys_to_element(pw_input, "pwpw1234").perform()
time.sleep(1)

#๋กœ๊ทธ์ธ ๋ฒ„ํŠผ ๋ˆ„๋ฅด๊ธฐ
loginButton = driver.find_element(By.ID, "log.login")
ActionChains(driver).click(loginButton).perform()

๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ์™€ ์›๋ฆฌ๋Š” ๋น„์Šทํ•˜๋‹ค.

๋˜‘๊ฐ™์ด ActionChains๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ์ด๋ฒˆ์—” send_keys_to_element ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

send_keys_to_element์— ์ž…๋ ฅํ•  ํƒœ๊ทธ์™€ ์ž…๋ ฅํ•  ๋ฌธ์ž๋ฅผ ์ธ์ž๋กœ ์ฃผ๊ณ , perform() ๋ฉ”์„œ๋“œ๋ฅผ ๋™์ž‘ํ•˜๋ฉด ๋œ๋‹ค.

ํด๋ฆญ ์™ธ์— ๋‹ค๋ฅธ ๋™์ž‘์€ https://www.selenium.dev/documentation/webdriver/actions_api/keyboard/ ์ฐธ์กฐ!

 

๋งˆ์šฐ์Šค ํด๋ฆญ๊ณผ ํ‚ค๋ณด๋“œ ์ž…๋ ฅ์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ, ๋„ค์ด๋ฒ„์—์„œ ๋กœ๊ทธ์ธ์„ ์‹œ๋„ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ์ง€๋งŒ..

 

๋„ค์ด๋ฒ„๋Š” ํ˜ธ๋ฝํ˜ธ๋ฝํ•˜์ง€ ์•Š์•˜๋‹ค..

 

์–ด์จ‹๋“  ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์šฐ๊ณ  ๋™์ž‘ํ•˜๋Š” ๊ฒƒ๋„ ํ™•์ธํ–ˆ์œผ๋‹ˆ ์—ฌ๊ธฐ์„œ ๋งˆ๋ฌด๋ฆฌ! ๐Ÿ‘‹

 


 

 

 

'TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[TIL]KDT_20230424  (0) 2023.04.24
[TIL]KDT_20230421  (0) 2023.04.21
[TIL]KDT_20230419  (0) 2023.04.19
[TIL]KDT_20230418  (1) 2023.04.18
[TIL]KDT_20230417  (0) 2023.04.17