웹크롤링 - . 웹사이트 자동 조작하기

12. 웹사이트 자동 조작하기

  • element 클릭: element.click()
  • element 더블 클릭: element.double_click()
  • element 키보드 입력 전송: element.send_keys()
  • element 로 마우스 이동: element.move_to_element()
from selenium import webdriver
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains

driver = webdriver.PhantomJS('C:/dev_python/phantomjs-2.1.1-windows/bin/phantomjs.exe')
# driver = webdriver.PhantomJS('/usr/local/Cellar/phantomjs/2.1.1/bin/phantomjs')
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.close()
Hello there, Doky Kim!
  • ActionChains(): 행동 여러 개를 체인 으로 묶어서 저장하고 원하는 만큼 실행
  • perform() 메서드 실행시 전체 행동을 실행함
from selenium import webdriver
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains

driver = webdriver.PhantomJS('C:/dev_python/phantomjs-2.1.1-windows/bin/phantomjs.exe')
# driver = webdriver.PhantomJS('/usr/local/Cellar/phantomjs/2.1.1/bin/phantomjs')
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.close()
Hello there, Doky Kim!
**연습문제4**
- 다음 코드를 기반으로, 코드를 추가하여 해당 다음 뉴스 댓글을 모두 출력하는 프로그램 작성하기
from selenium import webdriver
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

# 드라이버 생성
# 1. PhantomJS 웹브라우저 설치 및 파이썬 사용법
# - Installation of PhantomJS: MAC - pip install phantomjs, Window - http://phantomjs.org
# - Add the location of phantomjs to webdriver.PhantomJS('this')
chromedriver = 'C:/dev_python/Webdriver/chromedriver.exe'
driver = webdriver.Chrome(chromedriver)
driver.get("http://v.media.daum.net/v/20170922175202762")

print(driver.current_url)
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "alex-area"))
    )
finally:
    loop = True
    while loop:
        try:
            element = WebDriverWait(driver, 3).until(
                [------------------------------------]
            )
            [------------------------------------]
            webdriver.ActionChains(driver).move_to_element(more_button).click(more_button).perform()
        except:
            loop = False