陈旧元素错误python selenium webdriver

时间:2020-03-18 14:39:13

标签: python selenium selenium-webdriver

我正在尝试从以下链接中抓取一些数据: https://www.zacks.com/earnings/earnings-calendar

我在给定的日历选项中搜索日期,然后单击该日期。但是,当我尝试单击下载时,出现以下错误:

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

我试图检查解决方案,但是这些解决方案都不适合我。

这是我的代码的摘录。

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="date_select"]'))).click()
word = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="minical_place_holder"]/table/tbody/tr[1]/td[2]'))).text

    month_displayed = word[:-5] 
    year_displayed = word[-4:]
    print("month displayed:",month_displayed)
    print("year displayed:" ,year_displayed)

    #take year and compare
    #while desired year is less than the year currently displayed, click left button till you get that year. it will be in december
    if(str(desired_year) < year_displayed):
        while(str(desired_year) < year_displayed):
            #click left button till you get 2018
            print("gonna click button")
            WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="prevCal"]'))).click()
            word = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="minical_place_holder"]/table/tbody/tr[1]/td[2]'))).text
            month_displayed = word[:-5] 
            year_displayed = word[-4:]
            print("month displayed now:",month_displayed)
            print("year displayed now:" ,year_displayed)


desired_day =desired_date_object.strftime("%d")
        if("0" in desired_day[0]):
            desired_day[0] = desired_day[0].replace('0', '')
        day_id = "dt_"+desired_day
        print("day_id is: ",day_id)
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID,day_id))).click()
        print("got the day!")

        select = Select(driver.find_element_by_name('earnings_rel_data_all_table_length'))
        select.select_by_visible_text('ALL')

        #download here
        # driver = webdriver.Chrome(executable_path=chromepath, options=options)
        print(driver.page_source)
        return
        try:
            WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="earnings_rel_data_all_table_wrapper"]/div[2]/a[2]'))).click()

基本上,代码的作用是输入我输入的日期并检测年和月,因此它在日历上单击左或右,直到到达我的日期并单击日期。

一旦我到达页面,当我尝试单击下载时,就会出现错误。请帮我。任何帮助将不胜感激。

0 个答案:

没有答案