Selenium Python脚本,InvalidElementStateException [英] Selenium Python Script, InvalidElementStateException

查看:478
本文介绍了Selenium Python脚本,InvalidElementStateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在运行完全相同的测试时,我经常会收到此错误.

So I'm getting this error every so often when running the exact same test.

StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=69.0.3497.100)
(Driver info: chromedriver=2.41.578706 (5f725d1b4f0a4acbf5259df887244095596231db),platform=Mac OS X 10.12.6 x86_64)

唯一的问题是,它似乎在代码的不同区域不一致地发生.在尝试访问我的ReactJS页面的DOM元素(例如搜索字段)时.我通过SeleniumLibrary和自定义库的混合,通过ROBOT自动化框架来运行它.

The only problem is that it seems to happen inconsistently to different areas of the code. It's when trying to access DOM elements, like a search field, of my ReactJS page. I'm running this through ROBOT Automation Framework, using a mix of the SeleniumLibrary and a custom library.

我已经读到它听起来很简单,xPath在DOM上已经过时,但这无助于我弄清楚为什么在任何地方几乎任何地方都发生不一致的错误.

I've read that it's simply as it sounds, the xPath as become outdated on the DOM, but that doesn't help me figure out why it's an inconsistent error happening almost anywhere at any point.

这似乎正在发生:

def filter_modality(self, filter):
    filter_value = '//span[@title="{}"]//parent::li'.format(filter)

    self.selib.click_element(filter_locator)
    self.selib.wait_until_page_contains_element('//*[@class="multi-selector-options open"]')

    self.selib.wait_until_element_is_visible(filter_value)
    self.selib.click_element(filter_value )
    self.selib.wait_until_page_contains_element('//div...[@class="option selector-item active"]',
                                                error=("Could not select filter: {}".format(filter_value )))

    #I get the stale element message from or after executing this click_element
    self.selib.click_element(filter_locator)
    self.selib.wait_until_page_does_not_contain_element('//*[@class="multi-selector-options open"]', 
                                                        error="Filter dropdown did not disappear after selection")

推荐答案

当SE找到一个元素,但是不久之后(JS函数)已经对其进行了更改-从DOM中将其删除/重新排列,就会发生异常.显着更改了其属性,因此不再作为同一元素进行跟踪.
这是因为SE建立了DOM的内部缓存,该缓存可能与实际的缓存不同步.因此,名称为陈旧的"-埃尔.已以某种状态缓存,但其实际形式现在有所不同.

The exception comes when SE has found an element, but shortly afterwards something (a JS function) has changed it - removed from/rearranged it in the DOM, or changed its attributes significantly, so it's no longer tracked as the same element.
This comes for the fact that SE builds an internal cache of the DOM, which may become desynchronized from the actual one; thus the name "stale" - the el. is cached in some state, but its actual form is now different.

问题很常见,有一个特定的SO标签- https://stackoverflow.com/questions/tagged/staleelementreferenceexception (对此我感到很惊讶).

The issue is that common, that there is a specific SO tag for it - https://stackoverflow.com/questions/tagged/staleelementreferenceexception (I myself was surprised from this).

常见的解决方案是:

  • 在您知道会导致此问题的事件发生前先睡几秒钟
  • 在使用元素之前重新获取它(如果您将对它的引用存储在WebElement对象中,而使用robotframework并不是这种情况)
  • 具有重试机制来处理可能会导致执行的元素
  • 吞下异常并继续前进(我已经在几个地方做到了,那里的元素只是一个操作已执行的确认-SE曾经显示/发现过一次,之后我不在乎是什么DOM中发生了变化)
  • sleep for some seconds before an event you know will cause the issue
  • re-get an element before its usage (if you store a reference to it in a WebElement object, not really the case with robotframework)
  • have a rertry mechanism on working with an element that you know may cause the execution
  • swallow the exception and move along (I've done it in a few places, where the element was just a confirmation an operation was executed - it has been shown/found by SE once, afterwards I don't care is it changed in the DOM)

这篇关于Selenium Python脚本,InvalidElementStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆