활용3. 웹스크래핑(0825-0829)/selenium

스크래핑 iframe 처리 (네이버 홈: 우측 쇼핑몰) selenium

나도초딩 2022. 8. 29.

셀레니움 접속 후, 엘리먼트를 찾아도 안되는 경우들이 있다. 네이버 홈화면 우측에 보이는 쇼핑몰들이 대표적인데,

이럴 때는 iframe 이동처리를 해야함

 

네이버 쇼핑

https://jaeseokim.dev/Python/python-Selenium%EC%9D%84-%EC%9D%B4%EC%9A%A9%ED%95%9C-%EC%9B%B9-%ED%81%AC%EB%A1%A4%EB%A7%81-%EA%B0%84%EB%8B%A8-%EC%82%AC%EC%9A%A9%EB%B2%95-%EB%B0%8F-%EC%98%88%EC%A0%9C/

from selenium import webdriver
import time
import random
# 다운받은 webdriver의 경로설정
driver = webdriver.Firefox(executable_path='D:/OneDrive - JaeSeo/blog/seleninum/python/webdriver/geckodriver.exe')
# 네이버로 이동
driver.get("https://www.naver.com")
driver.implicitly_wait(5)
driver.switch_to_frame('cnsv_shbx')
print(driver.find_element_by_xpath('/html/body/div/div[2]/div/div/div/div/div/ul/li[1]/a/p').text,"\n")
time.sleep(random.uniform(4,5))
for i in range(int(driver.find_element_by_xpath('/html/body/div/div[2]/div/div/div/div/div/div[1]/em').text.split('/')[1])-1):
    time.sleep(random.uniform(1,2))
    driver.find_element_by_xpath('//*[@id="prod_page_next"]').click()
    print(driver.find_element_by_xpath('/html/body/div/div[2]/div/div/div/div/div/ul/li[1]/a/p').text,"\n")
driver.quit()

 

댓글