【问题标题】:RSelenium StaleElementReference error in lapplylapply 中的 RSelenium StaleElementReference 错误
【发布时间】:2020-04-22 02:48:41
【问题描述】:

我正在尝试获取与 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)。在这种情况下,我的代码是单击网络链接的所有“展开”箭头,以便显示全文。但是这里关注的点击被包裹在一个 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

【问题讨论】:

    标签: r selenium web-scraping


    【解决方案1】:

    当您单击函数中的箭头列表项时,arrow 列表中的项的 selenium 引用将被刷新。这就是您获得staleElementException 的原因。请使用 xpath/get 元素获取循环中的特定元素,然后使用索引指向特定箭头项,然后单击它。

    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]
        })  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-23
      • 1970-01-01
      • 2018-07-12
      • 1970-01-01
      • 2018-11-22
      • 1970-01-01
      • 1970-01-01
      • 2017-12-03
      相关资源
      最近更新 更多