웹사이트 자동 조작하기
이해하기 쉽고, 장황하지 않은 자료를 기반으로 강의를 진행합니다.
PhantomJS 지원 종료 안내
PhantomJS는 현재 개발이 중단되었습니다. Headless Chrome/Firefox와 Selenium WebDriver(ChromeDriver/GeckoDriver) 또는 Playwright 등 최신 대안을 사용하세요.
12. 웹사이트 자동 조작하기¶
- element 클릭: element.click()
- element 더블 클릭: element.double_click()
- element 키보드 입력 전송: element.send_keys()
- element 로 마우스 이동: element.move_to_element()
In [20]:
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
from selenium.webdriver import ActionChains
options = Options()
options.add_argument("--headless=new")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
driver = webdriver.Chrome(options=options)
driver.get("http://pythonscraping.com/pages/files/form.html")
firstnameField = driver.find_element(By.NAME, "firstname")
lastnameField = driver.find_element(By.NAME, "lastname")
submitButton = driver.find_element(By.ID, "submit")
firstnameField.send_keys("Doky")
lastnameField.send_keys("Kim")
submitButton.click()
print(driver.find_element(By.TAG_NAME, "body").text)
driver.quit()
- ActionChains(): 행동 여러 개를 체인 으로 묶어서 저장하고 원하는 만큼 실행
- perform() 메서드 실행시 전체 행동을 실행함
In [19]:
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
from selenium.webdriver import ActionChains
options = Options()
options.add_argument("--headless=new")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
driver = webdriver.Chrome(options=options)
driver.get("http://pythonscraping.com/pages/files/form.html")
firstnameField = driver.find_element(By.NAME, "firstname")
lastnameField = driver.find_element(By.NAME, "lastname")
submitButton = driver.find_element(By.ID, "submit")
actions = ActionChains(driver) .click(firstnameField).send_keys("Doky") .click(lastnameField).send_keys("Kim") .send_keys(Keys.RETURN)
actions.perform()
print(driver.find_element(By.TAG_NAME, "body").text)
driver.quit()
연습문제
- 다음 코드를 기반으로, 코드를 추가하여 해당 다음 뉴스 댓글을 모두 출력하는 프로그램 작성하기
- 다음 코드를 기반으로, 코드를 추가하여 해당 다음 뉴스 댓글을 모두 출력하는 프로그램 작성하기
In [ ]:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
auto_header = Options()
auto_header.add_argument("--headless=new")
auto_header.add_argument("--no-sandbox")
auto_header.add_argument("--disable-dev-shm-usage")
driver = webdriver.Chrome(options=auto_header)
driver.get("http://v.media.daum.net/v/20170922175202762")
print(driver.current_url)
try:
comment_area = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "alex-area"))
)
try:
more_button = WebDriverWait(driver, 3).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "a.link_more"))
)
more_button.click()
print("추가 콘텐츠를 펼쳤습니다.")
except TimeoutException:
print("추가 버튼이 없습니다.")
except TimeoutException:
print("alex-area를 찾지 못했습니다.")
finally:
driver.quit()