RSelenium 抛出 StaleElementReference 错误 [英] RSelenium throwing StaleElementReference error

查看:21
本文介绍了RSelenium 抛出 StaleElementReference 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图抓取这个网站 link 使用硒.我已经成功抓取了页面上的大部分内容,但试图解决设施访问"和设施投诉".由于这两个按钮在我使用开发人员工具检查它们时都有一个 javascript href,因此我一直在使用 phantomjs 和 RSelenium.

I am trying to scrape this website link using RSelenium. I have successfully scraped most of the contents on the page but was trying to get through to the "facility visits" and "facility complaints". Since both of those buttons have a javascript href when I inspect them with developer tools I have been using phantomjs and RSelenium.

我可以通过幻像成功导航到页面,但是每当我尝试使用 $getElementText 从字段中提取文本时,我都会抛出以下错误:

I can successfully navigate to the page via phantom but whenever I try to extract the text from the fields using $getElementText, I get thrown the following error:

Selenium message:{"errorMessage":"Element does not exist in cache","request":{"headers":{"Accept":"application/json, text/xml, application/xml, */*","Accept-Encoding":"gzip, deflate","Host":"localhost:4444","User-Agent":"libcurl/7.53.1 r-curl/2.6 httr/1.2.1"},"httpVersion":"1.1","method":"GET","url":"/attribute/id","urlParsed":{"anchor":"","query":"","file":"id","directory":"/attribute/","path":"/attribute/id","relative":"/attribute/id","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/attribute/id","queryKey":{},"chunks":["attribute","id"]},"urlOriginal":"/session/c0f30500-55d0-11e7-96dd-3b147ee40d88/element/:wdc:1497974074536/attribute/id"}}

 Show Traceback
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

当我使用 $currentURL 和 $screenship(display = T) 时,它显示了正确的网站呈现和正确的链接.

and when I use $currentURL and $screenship(display = T) it shows the correct website rendered and the correct link.

我知道这与元素如何附加到 DOM 有关,但我不确定如何解决 R 中的问题

I know it has something to do with how elements are attached to the DOM but I am not sure how to resolve the issue in R

代码如下:

url <- "https://dhs.arkansas.gov/dccece/cclas/FacilityInformation.aspx?FacilityNumber=23516"
rd<-remoteDriver(browserName = 'phantomjs')

rd$open()

rd$navigate(url)

webElem<- rd$findElement(using="xpath", value = '//*[@id="ctl00_ContentPlaceHolder1_lbtnVisits"]')

webElem$clickElement()

webElem$findElements('css',"#aspnetForm > div.page > div.main")

webElem$getElementAttribute("id")

推荐答案

您可能会遇到 StaleElementReference 作为点击 webElem 的结果.

You are probably getting a StaleElementReference as a result of clicking the webElem.

webElem 元素在点击后很可能在 DOM 中被修改,因此如果您再次尝试使用"webElem,它不再附加到 DOM 并且被认为是陈旧的".

The webElem element is likely modified in the DOM after the click, so if you try to "use" webElem again, it is no longer attached to the DOM and is considered "stale".

一个简单的解决方法是在点击 webElem 后简单地重新定位它:

An easy fix is to simply re-locate webElem after it is clicked:

webElem <- rd$findElement(...
webElem$clickElement()
webElem <- rd$findElement(... # re-locate webElem
webElem$findElements('css',"#aspnetForm > div.page > div.main")

这篇关于RSelenium 抛出 StaleElementReference 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆