ν‹°μŠ€ν† λ¦¬ λ·°

728x90
λ°˜μ‘ν˜•
μ…€λ ˆλ‹ˆμ›€μ€ μ›Ή 크둀링을 ν•˜κ±°λ‚˜, μž‘μ—…μ„ μžλ™ν™”ν•˜κΈ° μœ„ν•΄ 자주 쓰이곀 ν•œλ‹€. λ˜ν•œ, 이 κΈ°λŠ₯을 ν™œμš©ν•˜μ—¬ κ΅¬ν˜„ν•œ μ›Ήμ˜ ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€λ₯Ό μžλ™μœΌλ‘œ μ‹€ν–‰ κ°€λŠ₯ν•˜λ„λ‘ λ§Œλ“€ 수 μžˆλ‹€. 이λ₯Ό μœ„ν•΄ 파이썬으둜 μ…€λ ˆλ‹ˆμ›€μ„ μ‚¬μš©ν•˜κΈ° μœ„ν•œ 방법에 λŒ€ν•΄ μ •λ¦¬ν•˜κ³ μž ν•œλ‹€.

 

μ„€μΉ˜

1. selenium

 νŒŒμ΄μ¬μ˜ 경우 `pip`κ°€ μžˆμœΌλ―€λ‘œ κ°„λ‹¨ν•˜κ²Œ μ„€μΉ˜ν•  수 μžˆλ‹€. μ„€μΉ˜ ν™˜κ²½μ€ Pycharm을 μ‚¬μš©ν•˜κ³  `venv`λ₯Ό μ‚¬μš©ν•˜λŠ” ν”„λ‘œμ νŠΈλ₯Ό κΈ°μ€€μœΌλ‘œ μ„€λͺ…ν•˜κ³ μž ν•œλ‹€.

 Pycharm ν•˜λ‹¨μ— `Terminal`을 클릭 ν•œ 후에, `pip install selenium`을 μž…λ ₯ν•˜λ©΄ κ°„λ‹¨νžˆ μ„€μΉ˜ν•  수 μžˆλ‹€.

 

2. web-driver

 κ°μž μ‚¬μš©ν•˜κ³ μž ν•˜λŠ” 웹에 따라, μ›Ή λ“œλΌμ΄λ²„ νŒŒμΌμ„ ν”„λ‘œμ νŠΈ 폴더 내에 μœ„μΉ˜μ‹œμΌœμ•Ό ν•œλ‹€. λ˜ν•œ μ›Ή λ“œλΌμ΄λ²„μ™€ ν˜„μž¬ λ‘œμ»¬μ— μ‚¬μš©μ€‘μΈ μ›Ήμ˜ 버전이 μΌμΉ˜ν•˜μ—¬μ•Ό ν•œλ‹€.

 ν¬λ‘¬μ˜ 경우 우츑 μƒλ‹¨μ˜ `점 μ„Έκ°œ λ²„νŠΌ`을 클릭 ν›„, `도움말 → Chrome 정보`λ₯Ό 톡해 버전을 확인할 수 μžˆλ‹€. μ‚¬μš© 쀑인 μ›Ή λΈŒλΌμš°μ €μ˜ 버전을 μ•Œμ•˜λ‹€λ©΄, μΌμΉ˜ν•˜λŠ” λ²„μ „μ˜ λ“œλΌμ΄λ²„λ₯Ό λ‹€μš΄ν•˜λ©΄ λœλ‹€.

 λ‹€μš΄μ΄ 된 λ“œλΌμ΄λ²„λ₯Ό ν”„λ‘œμ νŠΈ 폴더 내에 μœ„μΉ˜μ‹œν‚€λ©΄ λœλ‹€. 보톡 Pycharm ν”„λ‘œμ νŠΈμ˜ 경우 μœˆλ„μš°λ₯Ό κΈ°μ€€μœΌλ‘œ, `C:\Users\[UserName]\PychamProcjests\[ProjectName]`이 κ²½λ‘œμ΄λ‹€. λ”°λΌμ„œ ν•΄λ‹Ή κ²½λ‘œμ— μ›Ή λ“œλΌμ΄λ²„λ₯Ό μœ„μΉ˜μ‹œν‚€λ©΄ λœλ‹€.

 

3. μ„€μΉ˜λœ 크둬 ν™œμš©ν•˜κΈ°

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

 μ–Έμ œλΆ€ν„° μƒκ²ΌλŠ”μ§€ λͺ¨λ₯΄κ² μ§€λ§Œ, μœ„μ™€ 같이 `webdriver_manager`λ₯Ό ν™œμš©ν•˜λ©΄ 번거둭게 `web-driver`λ₯Ό λ‹€μš΄λ°›μ§€ μ•Šμ•„λ„ ν˜ΈμŠ€νŠΈμƒμ— μ„€μΉ˜λœ 크둬을 ν™œμš©ν•˜μ—¬ μ…€λ ˆλ‹ˆμ›€μ„ μ‹€ν–‰ν•  수 μžˆλ‹€. built-in λΌμ΄λΈŒλŸ¬λ¦¬κ°€ μ•„λ‹ˆκΈ°μ—, `pip install webdriver_manager`λ₯Ό 톡해 λ³„λ„λ‘œ μ„€μΉ˜ν•˜μ—¬μ•Ό ν•œλ‹€.

 

κ°„λ‹¨ν•œ ν…ŒμŠ€νŠΈ

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


class DefaultTest:
    def __init__(self):
        self.driver = webdriver.Chrome()
        # webdriver_managerλ₯Ό ν™œμš©ν•˜λŠ” 경우.
		# self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
        
    def teardown(self):
        self.driver.quit()

    def auto_test(self):
        self.driver.get("https://www.google.com/")
        self.driver.set_window_size(974, 1040)
        self.driver.find_element(By.NAME, "q").send_keys("test")
        self.driver.find_element(By.NAME, "q").send_keys(Keys.ENTER)


if __name__ == '__main__':
    test = DefaultTest()
    test.auto_test()

 μœ„μ˜ μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜μ˜€μ„ λ•Œ, ꡬ글에 testλΌλŠ” ν‚€μ›Œλ“œλ₯Ό κ²€μƒ‰ν•œλ‹€λ©΄ μ •μƒμ μœΌλ‘œ μ‚¬μš© κ°€λŠ₯ν•œ μƒνƒœμ΄λ‹€.

 

728x90
λ°˜μ‘ν˜•
λŒ“κΈ€
κΈ€ 보관함
μ΅œκ·Όμ— 올라온 κΈ€
μ΅œκ·Όμ— 달린 λŒ“κΈ€