Python
[Python] 크롬 제어하기 (Selenium)
jeylee
2024. 5. 28. 13:10
파이썬을 활용하여 가장 기본적인 크롬 브라우저를 제어하는 코드입니다.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
chrome_option = Options()
chrome_option.add_experimental_option("detach", True)
chrome_option.add_argument('--incognito') #시크릿 모드
driver = webdriver.Chrome(options=chrome_option)
driver.implicitly_wait(3)
driver.get(url='https://google.com')
# class name으로 찾기
driver.find_element(By.CLASS_NAME,'gLFyf')
# tag name으로 찾기
driver.find_element(By.TAG_NAME,'textarea')
# id로 찾기
driver.find_element(By.ID,'APjFqb')
# XPath로 찾기
driver.find_element(By.XPATH,'//*[@id="APjFqb"]')
# 클릭하기
driver.find_element(By.XPATH,'//*[@id="APjFqb"]').click()
# 값 입력하기
driver.find_element(By.XPATH,'//*[@id="APjFqb"]').send_keys("tistory")
# 키보드 입력하기
driver.find_element(By.XPATH,'//*[@id="APjFqb"]').send_keys(Keys.ENTER)
파이썬을 설치하고 Selenium 을 pip로 설치해주세요.
이전에는 크롬 드라이버가 필요했었는데 Selenium 4.x 버전 부터는 크롬 드라이버가 필요하지 않다고 하네요.
이 코드를 샘플로 사용해보고
-자동 로그인
-자동 출석체크
-자동 입력
다양하게활용해 보세요.
2020년에 사용하던 코드가 동작하지 않아 다시 찾아 만들어 봤습니다.