当前位置: 首页 > 知识库问答 >
问题:

Python Selenium StaleElementRe传染异常

孙泉
2023-03-14

我在其他帖子中看到了解决方案(主要是建议更长的等待时间),但我尝试过,但没有成功。

下面是我得到的错误:

Traceback (most recent call last):
  File "LobbyistsPrep.py", line 126, in <module>
    the_download = get_file(year, report, download_dir)
  File "LobbyistsPrep.py", line 28, in get_file
    Year.select_by_visible_text(year_text)
  File "C:\Python27\lib\site-packages\selenium\webdriver\support\select.py", lin
e 120, in select_by_visible_text
    self._setSelected(opt)
  File "C:\Python27\lib\site-packages\selenium\webdriver\support\select.py", lin
e 212, in _setSelected
    option.click()
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 501, in _execute
    return self._parent.execute(command, params)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 308, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py"
, line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale elemen
t reference: element is not attached to the page document
  (Session info: chrome=65.0.3325.181)
  (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d902
3f),platform=Windows NT 6.1.7601 SP1 x86_64)

以下是相关代码

def get_file(year_text, category, download_dir):
    # Store a list of files in the Downloads directory.
    # We will use this later to determine the filename of the the CSV we downloaded.
    downloads_before = os.listdir( download_dir )

    # Change the Year dropdown
    Year = Select(driver.find_element_by_name('ctl00$ctl00$ContentPlaceHolder$ContentPlaceHolder1$ddYear'))
    Year.select_by_visible_text(year_text)
    time.sleep(30)

    # Change the Expenditure Type dropdown
    Type = Select(driver.find_element_by_name('ctl00$ctl00$ContentPlaceHolder$ContentPlaceHolder1$ddExpType'))
    Type.select_by_visible_text(category)
    time.sleep(30)

    # Change the Report Month dropdown
    Month = Select(driver.find_element_by_name('ctl00$ctl00$ContentPlaceHolder$ContentPlaceHolder1$ddMonth'))
    Month.select_by_visible_text('-- All Available --')
    time.sleep(30)

    # Click the Export to CSV button (downloads the CSV file)
    driver.find_element_by_name('ctl00$ctl00$ContentPlaceHolder$ContentPlaceHolder1$btnExport').click()
    time.sleep(30)

    # Now that we have downloaded the file, lets check the Downloads directory again and compare.
    downloads_after = os.listdir( download_dir )
    downloads_change = set(downloads_after) - set(downloads_before)
    # If there is only one difference, then that file is the one we downloaded.
    if len(downloads_change) == 1:
        file_name = downloads_change.pop()
        file_path = download_dir + file_name
        return file_path
    # Otherwise, something went wrong: Either the number of files changed by MORE than one, or NOTHING was downloaded.
    else:
        return False

driver.get('http://mec.mo.gov/mec/Lobbying/Lob_ExpCSV.aspx')
time.sleep(30)

for report in reports_wanted:
    for year in years_wanted:
        the_download = get_file(year, report, download_dir)
        if the_download:
            if report == 'Group':
                print 'Downloaded ' + the_download + '. Adding to GROUP.  Report:\t' + year + '\t' + report
                group_files.append(the_download)
            else:
                print 'Downloaded ' + the_download + '. Adding to INDIV.  Report:\t' + year + '\t' + report
                files.append(the_download)
        else:
            print 'PROBLEM DOWNLOADING: \t' + year + '\t' + report

我们的时代。睡眠曾经是时间。睡眠(2)-我试着把它改为30,但也没用。

我对除虫刮刀还很陌生,这不是我造的,所以请温柔点。提前谢谢。

共有2个答案

周玺
2023-03-14

我只有一个java解决方案,但它可以工作。

如果引发异常,我将捕获它并重试:

boolean isElementFound = false;

while(!isElementFound){

  try{

     WebElement myElement = Driver.findElement(By.id("elementID"));
     isElementFound = true;   

  }catch(StaleElementReferenceException e){
    //nothing!
  } 
}
谭景福
2023-03-14

把对固定问题的评论转化为对他人的回答。

解决上述问题的最终方法是将Chromedriver更新为至少2.36,因为它们运行在Chrome build 65上,而Chromedriver 2.33的当前版本不支持Chromedriver:https://sites.google.com/a/chromium.org/chromedriver/downloads

通过保持这些最新信息或推荐的一对,您将遇到chromedriver下载登录页上描述的较少问题。

如果您正在寻找有关StaleElementReduce ceException的帮助

以下是wiki中的定义:

当对元素的引用现在“过时”时抛出。

陈旧意味着元素不再出现在页面的DOM上。

StaleElementReduce ceException的可能原因包括但不限于:

您不再位于同一页面上,或者该页面可能在元素定位后已刷新。

该元素可能已被移除并重新添加到屏幕,因为它已定位。例如正在重新定位的元素。当更新值并重建节点时,javascript框架通常会发生这种情况。

元素可能位于iframe或其他已刷新的上下文中。

请参考以下内容:

  • Selenium HQ页面
  • 维基
  • SO在这个问题上给出了一些很好的答案
 类似资料:
  • 问题内容: 我想在我的ajax请求完成后呈现我的组件。 在下面您可以看到我的代码 但是我收到下面的错误,因为我正在ajax请求的done方法内返回render。 有没有办法在开始渲染之前等待我的ajax请求结束? 问题答案: 有两种处理方法,您可以选择哪种方法取决于应该拥有数据的组件和加载状态。 将Ajax请求移至父级并有条件地渲染该组件: 将Ajax请求保留在组件中,并在加载时有条件地渲染其他内

  • 异常: 传递流程 基于上述注释观察并理解异常传递流程: <?php function g1() { throw new \Exception(); yield; } // a2 -> b2 -> (new AsyncTask(g1()))->begin(); function g2() { yield; throw new \Exception(); } //

  • 问题内容: 我的Flutter(Dart)RenderFlex像素溢出了。 渲染库的例外。 如何管理或应用滚动功能到我的应用页面视图,并避免Flutter出现诸如以下消息的渲染异常: RenderFlex在底部溢出了28个像素。 如果您有任何需要完整的日志来帮助我的信息,请在这里: 在热重装时,它会根据消息在底部显示黄色/黑色条纹。 我可以使用可滚动的小部件来管理吗?或者我可以声明其他方式来控制它

  • 问题内容: 是否有关于Java中异常传播的准则? 何时在方法签名中添加例外?例如:如果仅当缺少必要的程序资源时才引发异常,并且只能在顶层进行处理,那么是否可以将所有使用erring方法的方法传播给使用此异常的所有方法? 有没有好的做法?有不良做法吗? 很抱歉,如果我含糊不清,但是我只是在寻求有关异常的编程风格的(一般)建议。 问题答案: 过去对我有帮助的准则包括: 当方法无法处理异常时引发异常 ,

  • 问题内容: 什么是异常传播?我尝试使用Google,但找不到满意的结果。最好用Java来解释这一点。 问题答案: 令人惊讶的是,在Java教程页面中有关exception的解释。 异常从一个方法传播到另一个方法,直到调用被捕获为止。因此,如果调用,调用,调用,并且如果抛出异常,则除非这些方法之一捕获到该异常,否则该异常将从d传播到c到b传播到a。

  • 当异常被抛出到组播中时,Camel不会传播异常。 在以下设置下,从其beanref引发异常: 为什么我不能向父路由提出多播异常? 骆驼2.17-快照