
주요 학습내용
셀레니움(Selenium)
1. 'Selenium'이란
2. Selenium 기초
- Beautiful Soup만으로 해결할 수 없는 것
- 접근할 웹 주소를 알 수 없을 때
- 자바스크립트를 사용하는 웹페이지의 경우
- 웹 브라우저로 접근하지 않으면 안될 때
Selenium 사용하기 위해 설치 필요한 것


해당 버전 크롬 드라이브 설치
설치 확인
from selenium import webdriver
options.page_load_strategy = 'normal'
driver = webdriver.Chrome(options=options)
driver.get("https://www.naver.com")
- 실행 후 네이버 창이 자동으로 켜지면 잘 설치된 것
- 초반에 계속 오류가 떠서 selenium도 update하고 다 해봤지만 계속 오류가 떴다.
- 강의에서 알려준 내용은 훨씬 더 간단했지만, 계속 오류가 났기에... 공식 documentation을 참고하여 option객체를 사용하여 설치했다
driver.quit()
- 꺼주지 않으면, 계속 크롬 창이 새로 생성될수 있으니, 실행 후 꺼주는 것이 좋음
from selenium import webdriver
options.page_load_strategy = 'normal'
driver = webdriver.Chrome(options=options)
driver.get("https://pinkwink.kr/")
driver.maximize_window()
driver.minimize_window()
driver.set_window_size(600, 800)
driver.refresh()
driver.back()
driver.forward()
from selenium.webdriver.common.by import By
first_content = driver.find_element(By.CSS_SELECTOR, '#content > div.cover-masonry > div > ul > li:nth-child(1)')
first_content.click()
driver.execute_script('window.open("")')
driver.execute_script('window.open("https://www.naver.com")')
driver.switch_to.window(driver.window_handles[1]) - 닫기(열려있는 탭 하나 닫음)
```javascript
driver.close()
1) 스크롤 가능한 높이 확인
driver.execute_script('return document.body.scrollHeight')
2) 최하단 이동
driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')
3) 최상단 이동
driver.execute_script('window,scrollTo(0, 0);')
4) 특정 태그 지점까지 스크롤 이동
from selenium.webdriver import ActionChains
some_tag = driver.find_element(By.CSS_SELECTOR, '#content > div.cover-thumbnail-list > div > h2')
action = ActionChains(driver)
action.move_to_element(some_tag).perform()
ActionChains
- ActionChains는 마우스 이동, 마우스 버튼 동작, 키 누르기 및 컨텍스트 메뉴 상호작용과 같은 낮은 수준의 상호작용을 자동화하는 방법
- 호버 및 드래그 앤 드롭과 같은 보다 복잡한 작업을 수행하는 데 유용
# ./ : 현재위치
driver.save_screenshot('./last_height.png')
참고자료 :