python3 selenium
python3 selenium 웹사이트 화면 스크롤 이동하기
Pymac
2023. 6. 9. 13:29
반응형
selenium으로 웹을 제어하다보면 화면을 벗어나 스크롤을 이동 할 경우가 발생한다.
스크롤을 이동하는 3가지 방법 xpath, class name, text로 요소를 찾고 스크롤 이동하기.
네이버 메인화면에 "뉴스홈" 으로 스크롤 이동하기 실습.
chrome 네이버 접속 후 개발자 모드에서 "뉴스홈" Class name, xpath, text를 확인하고 아래 소스코드에 대입.
기본적인 소스코드는 동일.
1.class name
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
options = Options()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=options)
element = driver.find_element(By.CLASS_NAME, "ContentHeaderView-module__tab_list___BWrWe")
driver.execute_script("arguments[0].scrollIntoView(true);", element)
2. xpath
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
options = Options()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=options)
element = driver.find_element(By.XPATH, '//*[@id="newsstand"]/div[1]/div/ul')
driver.execute_script("arguments[0].scrollIntoView(true);", element)
3. text
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
options = Options()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=options)
element = driver.find_element(By.XPATH, "//*[contains(text(), '뉴스홈')]")
driver.execute_script("arguments[0].scrollIntoView(true);", element)
반응형