I am learning Web scraping through RSelenium and I having problem to click the link in a table. Here is the sample code showing my problem
rm(list=ls(all=TRUE))
gc()
library(RSelenium)
library(netstat)
library(rvest)
library(tidyverse)
library(data.table)
system("taskkill /im java.exe /f", intern=FALSE, ignore.stdout=FALSE)
driver <- rsDriver(browser=c("chrome"), chromever="106.0.5249.21")
remote_driver <- driver[["client"]]
remote_driver$navigate("http://delcorealestate.co.delaware.pa.us/pt/search/commonsearch.aspx?mode=address")
Agree_button <- remote_driver$findElement(using = 'id', value = "btAgree")
Agree_button$clickElement()
AddDA <- data.frame(ID = c(1,2), number = c("30", "3"), street = c("HIGHVIEW DR","FOREST RD"))
for (i in 1:nrow(AddDA)) {
CurrID <- AddDA$ID[i]
CurrNumber <- AddDA$number[i]
CurrStreet <- AddDA$street[i]
# Mimic the process of input N umber and Street
Number_textfield <- remote_driver$findElement(using = 'id', value = 'inpNumber')
Number_textfield$sendKeysToElement(list(CurrNumber))
Street_textfield <- remote_driver$findElement(using = 'id', value = 'inpStreet')
Street_textfield$sendKeysToElement(list(CurrStreet))
Search_button <- remote_driver$findElement(using = 'id', value = "btSearch")
Search_button$clickElement()
Sys.sleep(5)
# for ID =2, it will return multiple address and I need to select the one I need, but I donot know how to do it
# element <- remote_driver$findElement(using = "link text", value = "FOREST RD 3") %>% clickElement() # SearchResults
# element <- remote_driver$findElement(using = "class", value = "SearchResults") %>% clickElement() # SearchResults
# check all the tables and save the data for current step
# and them go back the search page and do next loop
# here is the step to go back to the search page for next loop
r_dropdown <- remote_driver$findElement(using = "link text", value = "Property Search Home")
remote_driver$mouseMoveToLocation(webElement = r_dropdown)
remote_driver$findElement(using = "link text", value = "Search by Address")$clickElement()
}
In above code, when I run ID =1, everything is good and I got to the page/that I need, as shown below enter image description here
But for ID =2, it will return multiple addresses, and I need to select the 'FOREST RD 3' to open that link and go to a page similar to what I got when I ran ID =1 enter image description here
I have two questions here:
1 how to select the 'FOREST RD 3'. I right-clicked the page, and 'Inspect' the element, I can see 'FOREST RD 3' there but didnot find href.
2 how to add a if-checking inside of the loop, so the code can handle different cases, i.e., return with only one address (e.g, ID =1) , or multiple address (eg., ID) where I need to select the right one before I extract the data
I am new to RSelenium and I tried to google the solution, but unsuccessful. I wish someone could help me on this matter and would really appreciate that. Thanks.