반응형
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)
반응형
'python3 selenium' 카테고리의 다른 글
python3 selenium 동일한 요소 여러개 제어하기 (1) | 2023.11.18 |
---|---|
Python3 selenium click이 되지 않을 때 JavaScript를 사용하여 요소를 클릭, element_to_be_clickable, iframe, ActionChains (1) | 2023.11.18 |
python3 selenium ChromeDriver service, options (0) | 2023.11.17 |
pyinstaller, webdriver 콘솔창 안뜨게하기 (0) | 2023.09.19 |
캠핑장 예약 매크로, 캠핏 예약 매크로 (27) | 2023.09.13 |