본문 바로가기

Python_Crawling/Crawling

[Selenium]Python Study - PPT Presentation Material - 3

반응형

 Headless?

- 창 숨김 모드

- 윈도우 기준 크롬 59, /리눅스 기준 크롬 60버전

- CLI기반의 서버 OS에서도 Selenium을 통한 크롤링 / 테스트를 가능

Headless 사용

-선언

options.add_argument('headless')

-옵션 지정

options=webdriver.ChromeOptions()

driver=webdriver.Chrome('chromedriver',chrome_options=options)

-> 에러 발생시

driver=webdriver.Chrome('chromedriver',options=options)

from selenium import webdriver

driver = webdriver.Chrome('C:\\chromedriver\\chromedriver')

driver.get('http://naver.com')
driver.implicitly_wait(3)
driver.get_screenshot_as_file('naver_main.png')

driver.quit()

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('window-size=1920x1080')
options.add_argument("disable-gpu")

driver = webdriver.Chrome('C:\\chromedriver\\chromedriver', options=options)

driver.get('http://naver.com')
driver.implicitly_wait(3)
driver.get_screenshot_as_file('naver_main_headless.png')

driver.quit()

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains

options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome('C:\\chromedriver\\chromedriver', 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('An')\
    .click(lastnameField).send_keys('Kiwoong')\
    .send_keys(Keys.RETURN)

actions.perform()

print(driver.find_element_by_tag_name('body').text)

driver.quit()
반응형