R-Selenium,如何克服Noelement异常并继续执行

时间:2015-07-15 12:30:35

标签: r selenium xpath

我是R-selenium网络驱动程序的新手。我有任务要完成,我必须从以下网站下载所有Excel文件。http://lus.dacnet.nic.in/dt_lus.aspx

我正在使用R-Selenium Webdriver来自动化该过程。附件是代码段。

library(XML)
library(RSelenium)
library(dplyr)
library(magrittr)

Start Selenium Server ------------------------------------------------------

checkForServer()
startServer()
remDrv <- remoteDriver()
remDrv$open()

# Simulate browser session and fill out form ------------------------------

remDrv$navigate('http://lus.dacnet.nic.in/dt_lus.aspx')

#Select Year

remDrv$findElement(using = "xpath", 
               "//select[@name = 'DropDownList2']/option[@value = '1999-00']")$clickElement()

#Select Format

remDrv$findElement(using = "xpath", 
               "//select[@name = 'DropDownList3']/option[@value =  '2']")$clickElement()

#Select Element

district_IDs <- remDrv$findElements(using = "xpath",
                            "//select[@name = 'DropDownList1']/option") %>%
  lapply(function(x){x$getElementAttribute('value')}) %>% 
  unlist

district_names <- remDrv$findElements(using = "xpath",
                                "//select[@name = 'DropDownList1']/option")       %>%
 lapply(function(x){x$getElementText()}) %>% 
  unlist

# Retreive and Download

for (i in seq_along(district_IDs)) {

remDrv$findElement(using = "xpath",
                 paste0("//select[@name = 'DropDownList1']/option[@value =   ", 
                   "'", district_IDs[i], "']"))$clickElement()

#1.Total Area and Classification of Area  
  webElem <- remDrv$findElement(using = "xpath", 
               "//td[@class = 'TreeView1_2 TreeView1_8']/a[@id =  'TreeView1t2']")$clickElement() 

执行在此处停止在第8个状态并返回Noelement Exception并终止

#2.1 Sourcewise Area Irrigated                 
webElem2 <- remDrv$findElement(using = "xpath", 
               "//td[@class = 'TreeView1_2 TreeView1_8']/a[@id =  'TreeView1t4']")$clickElement()
Sys.sleep(10)

remDrv$goBack()
}   
remDrv$quit()
remDrv$closeServer()

该代码适用于8个州,我可以自动下载excel文件但是当它到达第8个州时(Dadranagarahavelli)它会返回Elementnotfound Exception。

错误讯息: -

Summary: NoSuchElement
Detail: An element could not be located on the page using the given   search parameters.
class: org.openqa.selenium.NoSuchElementException

任何帮助都将非常感谢!!!

0 个答案:

没有答案
相关问题