0

When I run below Program, Code "option2[opt2].click()" work well. But second "option2[opt2].click()" occur "StaleElementReferenceException"

I tried to solve this problem with "time.sleep(), implicitly_wait(), WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.XPATH, {Xpath}).

But Doesn't work. What is the problem........

#If you run my program, you can understand my code easily. I am Beginer in Programming and Korean. If explanation is not enough... sorry :( Please help me........

from selenium import webdriver
import pandas as pd
import time
import random

options = webdriver.ChromeOptions()
options.add_argument("user-agent={myagent}")
browser = webdriver.Chrome(options = options)
browser.get("https://yeyak.seoul.go.kr/web/main.do") #첫페이지로
browser.maximize_window()

#노원구 공간시설, 문화체험, 교육강좌 카테고리 크롤링
#column 이름
all_column_name = ['구', '카테고리', '대상', '장소', '이용기간', '접수기간', '선별방법',
               '모집정원', '신청제한', '취소기간', '이용요금', '예약방법', '문의전화']
all_data = []

#노원구 선택(이미 Click되어있는데, 또 Click을 하면 오류가 남)
browser.find_element_by_xpath("//*[@id=\"sch_loc\"]/option[11]").click()
time.sleep(1)
for i in range(2,5):
    #공간시설, 문화체험, 교육강좌 카테고리 선택
    browser.find_element_by_xpath(f"//*[@id='contents']/div[1]/div[1]/div/div/div[1]/ul/li[{i}]").click()
    time.sleep(1)
    category_name = browser.find_element_by_xpath(f"//*[@id='contents']/div[1]/div[1]/div/div/div[1]/ul/li[{i}]").text
    district = browser.find_element_by_xpath("//*[@id=\"contents\"]/div[1]/div[1]/div/div/div[2]/div[3]/div[1]/select/option[11]").text #구 이름 district 저장
    #시설 선택 버튼_소주제
    button2 = browser.find_element_by_xpath("//*[@id=\"contents\"]/div[1]/div[1]/div/div/div[2]/div[3]/div[2]/select")
    button2.find_elements_by_tag_name("option")
    option2 = button2.find_elements_by_tag_name("option")
    if len(option2) == 1: #만약 옵션이 별도로 없는 경우 다음 카테고리로 넘어가기
        continue
    for opt2 in range(1, len(option2)+1):
        print(option2[opt2].text)
        option2[opt2].click()
        time.sleep(1)
        #시설 상세 선택 버튼
        button3 = browser.find_element_by_xpath("//*[@id=\"contents\"]/div[1]/div[1]/div/div/div[2]/div[3]/div[3]/select")
        option3 = button3.find_elements_by_tag_name("option")
        if len(option3) == 1: #만약 옵션이 별도로 없는 경우 다음 카테고리로 넘어가기
            continue
        for opt3 in range(1, len(option3)+1):
            small_data = []
            option3[opt3].click()
            time.sleep(1)
            #예약하기 버튼 클릭
            browser.find_element_by_xpath("//*[@id=\"contents\"]/div[1]/div[1]/div/div/div[2]/div[3]/button").click()
            time.sleep(1)
            #테이블에서 데이터 가져오기
            table = browser.find_element_by_xpath("//*[@id=\"aform\"]/div[1]/div[2]/ul")
            rows = table.find_elements_by_tag_name("li")
            small_data.append(district) # 구 이름 넣기
            small_data.append(category_name) # 카테고리 이름 넣기
            for row in rows:
                small_data.append(row.text.split("\n")[1])
            all_data.append(small_data)
            time.sleep(random.uniform(2,3))
            browser.get("https://yeyak.seoul.go.kr/web/main.do")
            time.sleep(random.uniform(2,3))
            browser.find_element_by_xpath("//*[@id=\"sch_loc\"]/option[11]").click() #노원구 클릭
            time.sleep(random.uniform(2,3))
            browser.find_element_by_xpath(f"//*[@id='contents']/div[1]/div[1]/div/div/div[1]/ul/li[{i}]").click() #카테고리 클릭
            time.sleep(random.uniform(2,3))
            browser.find_element_by_xpath(f"//*[@id=\"contents\"]/div[1]/div[1]/div/div/div[2]/div[3]/div[2]/select/option[{opt2}]")
            time.sleep(random.uniform(2,3))
            if opt3 == len(option3)+1:
                break
  • I think the issue is when clicking the first time your page is moving to another url and then your options will be stale losing it's values. – Arundeep Chohan Jan 29 '22 at 20:09
  • Does this answer your question? [stale element reference: element is not attached to the page document](https://stackoverflow.com/questions/18225997/stale-element-reference-element-is-not-attached-to-the-page-document) – NKSM Feb 08 '22 at 08:07

0 Answers0