首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Selenium StaleElementReferenceException

Python Selenium StaleElementReferenceException
EN

Stack Overflow用户
提问于 2018-04-05 02:18:52
回答 2查看 158关注 0票数 2

我已经在其他帖子上看到了解决这个问题的方法(主要是建议更长的等待时间),但我尝试过,但没有成功。

下面是我得到的错误:

代码语言:javascript
复制
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)

相关代码如下:

代码语言:javascript
复制
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

我们的time.sleep曾经是time.sleep(2) --我试着把它改成30,但也没有用。

我还是个清除窃听器的新手,而且这个不是我造的,所以请温柔点。提前谢谢。

EN

回答 2

Stack Overflow用户

发布于 2018-04-05 06:51:37

将已修复问题的评论转化为对他人的回答。

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

通过保持这些更新,或者在推荐的pair上,您将遇到更少的问题,如chromedriver下载登录页面中所述。

如果您正在寻求有关StaleElementReferenceException的帮助

这是来自wiki的定义

当对元素的引用现在是“陈旧的”时抛出

陈旧表示元素不再出现在页面的DOM上。

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

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

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

元素可能位于已刷新的iframe或另一个上下文中。

请参考以下内容:

在那个问题上

票数 0
EN

Stack Overflow用户

发布于 2018-04-05 23:31:33

我只有一个java的解决方案,但它是有效的。

如果抛出异常,我会捕获它并重试:

代码语言:javascript
复制
boolean isElementFound = false;

while(!isElementFound){

  try{

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

  }catch(StaleElementReferenceException e){
    //nothing!
  } 
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49657833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档