Selenium 循环页面在 python 后刷新

Selenium loop page refreshed in python

我有一些关于在 Python 中使用 Selenium 进行循环的问题。事实上,我想遍历 'driver.find_elements_by_id' 跟踪的 link 的列表,然后一个一个地点击它们,但问题是每次我点击 link ('linklist' 在代码中),页面被刷新,因此有一条错误消息表明 'Message: The element reference is stale. Either the element is no longer attached to the DOM or the page has been refreshed.'

我知道是因为点击后link的列表消失了。但是,即使页面不再存在,我通常如何在 Selenium 中迭代列表。我使用了 'driver.back()',但显然它不起作用。

代码中这一行后弹出错误信息:

link.click()  

link列表位于这个URL(我想在按钮Document上clink然后在刷新页面显示后下载第一个文件)'https://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&CIK=0001467373&type=10-K&dateb=20101231&owner=exclude&count=40'

有人可以看看这个问题吗? 谢谢!

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import unittest
import os
import time
from bs4 import BeautifulSoup
from selenium.webdriver.common.keys import Keys
import requests
import html2text



class LoginTest(unittest.TestCase):
 def setUp(self):


    self.driver=webdriver.Firefox()
    self.driver.get("https://www.sec.gov/edgar/searchedgar/companysearch.html")


 def test_Login(self):
    driver=self.driver

    cikID="cik"
    searchButtonID="cik_find"
    typeID="//*[@id='type']"
    priorID="prior_to"
    cik="00001467373"
    Type="10-K"
    prior="20101231"
    search2button="//*[@id='contentDiv']/div[2]/form/table/tbody/tr/td[6]/input[1]"


    documentsbuttonid="documentsbutton"
    formbuttonxpath='//a[text()="d10k.htm"]'


    cikElement=WebDriverWait(driver,30).until(lambda driver:driver.find_element_by_id(cikID))

    cikElement.clear()
    cikElement.send_keys(cik)


    searchButtonElement=WebDriverWait(driver,20).until(lambda driver:driver.find_element_by_id(searchButtonID))
    searchButtonElement.click()

    typeElement=WebDriverWait(driver,30).until(lambda driver:driver.find_element_by_xpath(typeID))
    typeElement.clear()
    typeElement.send_keys(Type)
    priorElement=WebDriverWait(driver,30).until(lambda driver:driver.find_element_by_id(priorID))
    priorElement.clear()
    priorElement.send_keys(prior)
    search2Element=WebDriverWait(driver,30).until(lambda driver:driver.find_element_by_xpath(search2button))
    search2Element.send_keys(Keys.SPACE)
    time.sleep(1)

    documentsButtonElement=WebDriverWait(driver,20).until(lambda driver:driver.find_element_by_id(documentsbuttonid))
    a=driver.current_url



    window_be1 = driver.window_handles[0]
    linklist=driver.find_elements_by_id(documentsbuttonid)


    with open("D:/doc2/"+"a"+".txt", mode="w",errors="ignore") as newfile:


        for link in linklist:

                link.click()            

                formElement=WebDriverWait(driver,30).until(lambda driver:driver.find_element_by_xpath(formbuttonxpath))
                formElement.click()
                time.sleep(1)

                t=driver.current_url

                r = requests.get(t)
                data = r.text

                newfile.write(html2text.html2text(data))

                drive.back()
                drive.back()


 def terdown(self):
    self.driver.quit()
if __name__=='__main__':
 unittest.main()

您不应使用网络元素列表,而应使用 link 列表。尝试这样的事情:

linklist = []
for link in driver.find_elements_by_xpath('//h4[@class="title"]/a'):
    linklist.append(link.get_attribute('href'))

然后您可以遍历 link 的列表

for link in linklist:
    driver.get(link)
    # do some actions on page

如果您想要物理点击每个 link,您可能需要使用

for link in linklist:
    driver.find_element_by_xpath('//h4[@class="title"]/a[@href=%s]' % link).click()
    # do some actions on page