Python中的Selenium中的陈旧元素引用异常

时间:2018-08-13 22:43:13

标签: python-3.x selenium selenium-webdriver web-scraping beautifulsoup

我正在尝试从https://www.poets.org/poetsorg/poems抓取不同诗歌的链接,但是我收到了Stale Element Reference Exception错误消息。我尝试增加睡眠时间和WebDriverWait都没有成功。任何帮助将不胜感激。我的代码如下。

def poem_scraper(url):
    driver = webdriver.Chrome("\chromedriver.exe")
    driver.get(url)
    all_links = []
    for _ in range(10):
        soup = BeautifulSoup(driver.page_source,"html.parser")
        total_poems = soup.find_all('td',attrs={'class':"views-field views-field-title"})
        for div in total_poems:
            links = div.find_all('a')
            for a in links:
                all_links.append('https://www.poets.org'+a['href'])

        timeout = 15
    #time.sleep(6)
        try:

            element_present = EC.presence_of_element_located((By.LINK_TEXT, 'next'))
            WebDriverWait(driver, timeout).until(element_present)
        except TimeoutException:
            print("Timed out waiting for page to load")

        test = driver.find_element_by_link_text('next')
        time.sleep(6)
        test.click()
    return(all_links)

StaleElementReferenceException:消息:stale元素引用:元素未附加到页面文档

0 个答案:

没有答案