我正在尝试使用RSelenium抓取此网站link。我已经成功地抓取了页面上的大部分内容,但试图进入“设施访问”和“设施投诉”。由于当我使用开发人员工具检查这两个按钮时,这两个按钮都有一个javascript href,我一直在使用phantomjs和RSelenium。
我可以通过幻像成功导航到该页面但是每当我尝试使用$ getElementText从字段中提取文本时,我都会抛出以下错误:
String key = String.join(":", type, subType, id) ;
productMap.put(key, product) ;
当我使用$ currentURL和$ screenship(display = T)时,它显示正确的网站呈现和正确的链接。
我知道它与元素如何附加到DOM有关但我不知道如何解决R中的问题
以下代码:
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
答案 0 :(得分:2)
点击webElem
后,您可能会获得StaleElementReference
。
点击后可能会在DOM中修改webElem
元素,因此如果您再次尝试“使用”webElem
,它将不再附加到DOM并被视为“陈旧”。
一个简单的解决方法是在点击webElem
之后简单地重新找到它:
webElem <- rd$findElement(...
webElem$clickElement()
webElem <- rd$findElement(... # re-locate webElem
webElem$findElements('css',"#aspnetForm > div.page > div.main")