I am trying to extract all tables in the webpage. But when I extract the data, it shows only the heading of the table. Please help. I am extremely new to this. I am attached a picture of the results I am getting.
Website: https://pgems2018.wbsec.org/PublicPages/VotingResult2018.aspx
library(dplyr)
library(stringr)
library(purrr)
library(rvest)
library(RSelenium)
# run in terminal: docker run -d -p 4445:4444 selenium/standalone-chrome
remDr <- RSelenium::remoteDriver(remoteServerAddr = "localhost",
port = 4445L,
browserName = "chrome")
remDr$open()
# naviagte to the website
remDr$navigate("https://pgems2018.wbsec.org/PublicPages/VotingResult2018.aspx")
# Give some time load
Sys.sleep(4)
# Increase the window size to find elements
remDr$maxWindowSize()
remDr$screenshot(display = TRUE) #This will take a screenshot and display it in the RStudio viewer
# Read page source
source <- remDr$getPageSource()[[1]]
# Election type
list_type <- read_html(source) |>
html_nodes(css = "#ContentPlaceHolder1_cmbCandidateFor") |>
html_nodes("option") |>
html_text()
list_type <- list_type[-1]
# Zilla Parishad Name
district <- read_html(source) |>
html_nodes(css = "#ContentPlaceHolder1_cmbZillaParisadName") |>
html_nodes("option") |>
html_attr("value")
district <- district[-1]
# Click on election dropdown list
election_type <- remDr$findElement(using = "css selector",
value = "#ContentPlaceHolder1_cmbCandidateFor")
election_type$clickElement()
Sys.sleep(4)
# Select election type
ZillaP <- remDr$findElement(using = "css selector", value = "#ContentPlaceHolder1_cmbCandidateFor > option:nth-child(2)" )
ZillaP$clickElement() # selected Zilla Parishad
Sys.sleep(4)
# Preallocate districts
data_district <- vector("list", length(district))
# Iterate over districts
for (k in seq_along(district)){
# Open zilla parishad dropdown list
district_list <- remDr$findElement(using = "css selector",
value = "#ContentPlaceHolder1_cmbZillaParisadName")
district_list$clickElement
Sys.sleep(4)
#click corresponding zilla
district_current <- remDr$findElement(using = "css selector", value = str_c("select[id = ContentPlaceHolder1_cmbZillaParisadName] > option[value='", district[[k]], "']"))
district_current$clickElement
Sys.sleep(2)
# click on the search button
search_button <- remDr$findElement(using = "css selector",
value = "#ContentPlaceHolder1_btnSearch")
search_button$clickElement
Sys.sleep(4)
# Populate element of corresponding position (first page)
data_district[[k]] <- remDr$getPageSource()[[1]] |>
read_html() |>
html_table()
}