运行我的scrpt时无法摆脱“陈旧元素”错误

时间:2018-07-06 21:08:05

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

我用selenium用python写了一个脚本。该脚本应该单击网页上的某些链接。当我运行脚本时,它确实单击了第一个链接,然后抛出错误preConditions而不是追逐下一个链接。在过去的几个小时中,我进行了大量搜索,以找到解决该错误的方法,但是没有运气。

我对他们的数据不感兴趣,因此,除点击功能外,其他任何解决方案都不是我想要的。如何单击链接直到最后一个链接?

这是我到目前为止的尝试:

stale element reference: element is not attached to the page document

1 个答案:

答案 0 :(得分:1)

您可以尝试以下代码:

def click_links(driver,url):
    driver.get(url)
    links_len = len(wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, "result-row__item-hover-visualizer"))))
    for index in range(links_len):
        cookies_bar = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '[data-bind="visible: showCookieDialog"]')))
        driver.execute_script("arguments[0].hidden='true';", cookies_bar)
        wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[data-track="click-show-more"]'))).click()
        entry = wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, "result-row__item-hover-visualizer")))[index]
        entry.click()
        time.sleep(3)
        driver.back()
相关问题