我正在尝试与Rselenium建立链接。有时-仅有时并且这是不可复制的(因为当我重新运行代码时,问题消失了)-该程序给我一个如下错误:
Error: Summary: StaleElementReference
Detail: An element command failed because the referenced element is no longer attached to the DOM.
class: org.openqa.selenium.StaleElementReferenceException
Further Details: run errorDetails method
[我认为这是因为我单击了Web元素,并且单击后DOM进行了某种修改(请参阅此答案:RSelenium throwing StaleElementReference error)。在这种情况下,我的代码是单击Web链接的所有“展开”箭头,以便显示全文。但是,这里关注的点击被包裹在一个如下的sapply函数中,所以我不能每次都重新定位该web元素:
arrow = remDr$findElements(using = 'class', value = "WB_text_opt") #locate the arrows
sapply(arrow, function(x){
Sys.sleep(0.15)
x$clickElement()
}) # click on them
remDr$findElement('css', 'html')$sendKeysToElement(list(key = "end")) # scroll the webpage down
arrow
列表中各项的硒参考将在您单击功能中的箭头列表项时刷新。这就是您获得staleElementException
的原因。请使用xpath获取循环中的特定元素/获取元素,然后使用索引指向特定的箭头项,然后单击它。
sapply(arrow, function(x){
Sys.sleep(0.15)
x$clickElement() #<== This line will work only for the first iteration.
# you will get issue from the 2nd item as the element references
# updates, when you click on 1st item.
# Try using something like .findElements()[index]
})