RSelenium无法找到具有给定参数的元素

时间:2017-06-13 01:36:00

标签: r web-scraping rselenium

我使用RSelenium构建一个包含管理员信息的数据框。我在下拉列表中选择元素时遇到问题。

我的代码如下:

> require(RSelenium)
> remDr<-remoteDriver(browserName = "chrome")
> remDr$open()
> enlace<-'https://www.sisben.gov.co/atencion-al-ciudadano/Paginas/Directorio-administradores.aspx'
> remDr$navigate(enlace)
> remDr$findElement(using = "xpath", '//*[@id="ddlDepartamento"]/option[2]')$clickElement()

使用最后一行,我收到以下错误:

Selenium message:no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="ddlMunicipio"]"}
  (Session info: chrome=58.0.3029.110)
  (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 10.0.14393 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 140 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.2.0', revision: '8c03df6', time: '2017-03-02 09:34:51 -0800'
System info: host: 'PATY-FRAN', ip: '192.168.0.20', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_121'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9), userDataDir=C:\Users\victor\AppData\Local\Temp\scoped_dir6076_6551}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=58.0.3029.110, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
Session ID: 0815f4f9dcca9d364a7c15b4a50352e7
*** Element info: {Using=xpath, value=//*[@id="ddlMunicipio"]}

Error:   Summary: NoSuchElement
     Detail: An element could not be located on the page using the given search parameters.
     class: org.openqa.selenium.NoSuchElementException
     Further Details: run errorDetails method

我将非常感谢你的帮助。

1 个答案:

答案 0 :(得分:2)

内容位于iframe中。您需要先切换到iframe:

library(RSelenium)
rD<-rsDriver()
remDr <- rD$client
enlace<-'https://www.sisben.gov.co/atencion-al-ciudadano/Paginas/Directorio-administradores.aspx'
remDr$navigate(enlace)
# content is in iframe
frames <- remDr$findElements("css", "iframe")
# switch to first iframe
remDr$switchToFrame(frames[[1]])
selectElem <- remDr$findElement("id", "ddlDepartamento")
selectOpt <- selectElem$selectTag()
> selectOpt$text
[1] "AMAZONAS"           "ANTIOQUIA"          "ARAUCA"             "ATLANTICO"         
[5] "BOGOTÁ D.C."        "BOLIVAR"            "BOYACA"             "CALDAS"            
[9] "CAQUETA"            "CASANARE"           "CAUCA"              "CESAR"             
[13] "CHOCO"              "CORDOBA"            "CUNDINAMARCA"       "GUAINIA"           
[17] "GUAJIRA"            "GUAVIARE"           "HUILA"              "MAGDALENA"         
[21] "META"               "NARIÑO"             "NORTE DE SANTANDER" "PUTUMAYO"          
[25] "QUINDIO"            "RISARALDA"          "SAN ANDRES"         "SANTANDER"         
[29] "SUCRE"              "TOLIMA"             "VALLE"              "VAUPES"            
[33] "VICHADA"  

# click 2nd one
selectOpt$elements[[2]]$clickElement()

....
....
rm(rD)
gc()
相关问题