程序在调试模式下运行正常,但正常运行却出现错误

这是我当前的代码,但是每当我运行它时,最后一行都会出现错误


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

from selenium import webdriver


url = "https://otctransparency.finra.org/otctransparency/OtcDownload"

driver.get(url)

driver.maximize_window()

driver.implicitly_wait(5)


agree = driver.find_elements_by_xpath("//button[@class='btn btn-warning']")[0]

agree.click()

nonats = driver.find_element_by_link_text('OTC (Non-ATS) Download')

nonats.click()


driver.find_element_by_xpath("//img[@src='./assets/icon_download.png']").click()

driver.switch_to.window(driver.window_handles[0])


driver.find_element_by_xpath("(//div[@class='checkbox-inline'])[2]").click()

driver.find_element_by_xpath("(//div[@class='checkbox-inline'])[1]").click()


driver.implicitly_wait(5)

button = driver.find_element_by_xpath("//img[@src='./assets/icon_download.png']")

print(button.is_displayed())

button.click()

当我在调试模式下逐行运行代码时,一切正常,没有任何错误。任何帮助都会很棒。


编辑:这是我的堆栈跟踪


Traceback (most recent call last):

 File "C:\Users\derpe\Desktop\python projects personal\testing finra\untitled1.py", line 31, in <module>

    button.click()


  File "C:\Users\derpe\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click

    self._execute(Command.CLICK_ELEMENT)


  File "C:\Users\derpe\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute

    return self._parent.execute(command, params)


  File "C:\Users\derpe\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute

    self.error_handler.check_response(response)


  File "C:\Users\derpe\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response

    raise exception_class(message, screen, stacktrace)


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

  (Session info: chrome=86.0.4240.75)


明月笑刀无情
浏览 104回答 1
1回答

不负相思意

当您搜索下载链接时,单击复选框会触发页面刷新。调用sleep以允许刷新完成。driver.implicitly_wait(5)import time&nbsp; &nbsp;# add this&nbsp;time.sleep(1)&nbsp; &nbsp;# add thisbutton = driver.find_element_by_xpath("//img[@src='./assets/icon_download.png']")print(button.is_displayed())button.click()我尝试了其他硒等待,但它们对我不起作用。可能是因为元素搜索在刷新开始之前成功但仍然点击较晚。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python