【问题标题】:Stale element reference error?过时的元素引用错误?
【发布时间】:2017-08-02 17:47:08
【问题描述】:

我有这段 HTML 代码:

   <a class="button-color1 full" href="/AntecipacaoRecebiveis/AntecipacaoAutomatica">Simule e contrate</a>                        </div>
                        <div class="interntab clenitab" id="tabs-2">
                            <div class="row">
                                <div class="col-xs-8 col-sm-9 taxa-mes" style="text-align:center">
                                    <span class="caption-value">a ser creditado em sua conta</span><br />
                                    <span class="value-simulator">R$ 0,00</span>
                                </div>
                                <div class="col-xs-4 col-sm-3 a-ser-creditado" style="padding-top: 24px;">
                                    <span class="caption-value" style="font-size: 13px;line-height:6px">Taxa a.m.</span><br />
                                    <span class="value-simulator" style="font-size: 20px;line-height:8px">2,50%</span>
                                </div>
                            </div>

然后,我尝试通过单击 onclick="SetAdquirente('ADIQ') 来自动化我的测试(我在同一网站的许多其他步骤上都成功了),但它产生了以下错误:

raise exception_class(message, screen, stacktrace)

selenium.common.exceptions.StaleElementReferenceException:消息: 过时的元素引用:元素未附加到页面文档 (会话信息:chrome=59.0.3071.115)(驱动程序信息: 铬驱动程序=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),平台=Windows NT 6.1.7601 SP1 x86_64)

我找到它的代码是:

    elem = driver.find_element_by_id('tabs-2')
    action.move_to_element(elem)
    action.click()
    action.perform()

为什么会这样? (我尝试使用其他 find_element 语句,例如 xpath、链接文本等,结果相同)

非常感谢!

【问题讨论】:

标签: python python-3.x selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

当相关 web 元素在 dom 上发生更改并且对该 web 元素的初始引用丢失时,会发生 StaleElementException。

你可以再次搜索webelement

试试这个

try:
  elem = driver.find_element_by_id('tabs-2')
  action.move_to_element(elem)
  action.click()
  action.perform()
except StaleElementReferenceException:
 elem = driver.find_element_by_id('tabs-2')
 action.move_to_element(elem)
 action.click()
 action.perform()

【讨论】:

  • 谢谢你,@michael-satish。但这并不能解决我的问题。它返回相同的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-01
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 2019-03-20
  • 2014-12-03
相关资源
最近更新 更多