Lappy中的RSelenium StaleElement引用错误
我正在努力寻找与硒的联系。有时——只是有时,这是不可复制的(因为当我重新运行代码时,问题消失了)——程序会给我一个错误,如下所示:Lappy中的RSelenium StaleElement引用错误,r,selenium,web-scraping,R,Selenium,Web Scraping,我正在努力寻找与硒的联系。有时——只是有时,这是不可复制的(因为当我重新运行代码时,问题消失了)——程序会给我一个错误,如下所示: Error: Summary: StaleElementReference Detail: An element command failed because the referenced element is no longer attached to the DOM. class: org.openqa.selenium.StaleEle
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在点击后不知何故被修改了(请参见下面的答案:)。在本例中,我的代码是单击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
单击函数中的箭头列表项时,将刷新
箭头
列表中项目的selenium引用。这就是您得到staleElementException
的原因。请使用xpath/get-the-elements获取循环中的特定元素,然后使用索引指向特定的箭头项,然后单击它
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]
})
sapply(箭头,函数(x){
系统睡眠(0.15)
x$clickElement()#