RSelenium in Shiny app: can't pull html out of browser

Viewed 57

I am trying to create a shiny app that opens a web browser when a button is clicked, then when the 'Refresh' button is clicked, load the html from whatever site. This is a proof of concept for a more advanced project, but I cant get this to work. Sometimes the browser closes after 30 seconds (not due to timeout), but the main problem is that the refresh button does not update the displayed text; the entire app screen greys out.

library('shiny')
library('RSelenium')
library('dplyr')
library('rvest')

ui <- fluidPage(
  
  # Application title
  titlePanel("Title"),
  
  sidebarLayout(
    sidebarPanel(
      actionButton('open_browser','Open Browser')
      ,actionButton('refresh_html','Refresh')
    ),
    
    mainPanel(
      textOutput("text")
    )
  )
)

server <- function(input, output) {
  
  values<-reactiveValues()
  values$message_text<-'Open browser to begin'
  
  observeEvent(input$open_browser,{
    system("taskkill /im java.exe /f")
    rD <- rsDriver(browser="chrome", port=4566L, verbose=F, chromever = "90.0.4430.24")
    remDr <- rD[["client"]]
    remDr$navigate('https://google.com')
  })
  
  observeEvent(input$refresh_html,{
    html <- driver$getPageSource()[[1]]
    values$message_text<-read_html(html) %>% as.character()
  })
  
  output$text <- renderText({
    values$message_text
  })
}

shinyApp(ui = ui, server = server)
0 Answers
Related