我有我的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')
来自动化我的测试(我在同一网站的许多其他步骤上取得了成功(,但它生成了以下错误:
提高exception_class(消息、屏幕、堆栈跟踪(
selenium.common.exceptions.StaleElementReferenceException: Message: 过时的元素引用:元素未附加到页面文档 (会话信息:chrome=59.0.3071.115( (司机信息: Chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8(,platform=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、链接文本等,并引发相同的结果(
多谢!
当有问题的webelement在dom上被更改并且对该webelement的初始引用丢失时,就会发生StaleElementException。
您可以再次搜索网络元素
试试这个
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()