Search code examples
rweb-scrapingrselenium

Button Press/Loading Webpage in RSelenium


I'm trying to automate a search with RSelenium and struggling immensely. Everything seems to work, but whenever I run the code to press the search button, it seems to click but not actually search. I've scoured the history of RSelenium related questions here and I'm not finding a solution that works...any suggestions from the clever folks here? Reproducible example of the code below, just have to load the libraries and start your machine/driver of choice.

# Go to License Plate lookup page
mydriver$navigate("https://checktoprotect.org/vin-check/")

# Enter state
dropdown_element <- mydriver$findElement(using = "xpath", value='//*[@id="state-select"]/div[1]')
dropdown_element$clickElement()
state_element <- mydriver$findElement(using = "xpath", value='//*[@id="state-select"]/div[2]/div[23]')
state_element$clickElement()

# Enter License Plate
plate_element <- mydriver$findElement(using = "xpath", value='//*[@id="js-vin-input"]')
plate_element$sendKeysToElement(list("ELX1701"))

# Click submit
submit1_element <- mydriver$findElement(using = 'xpath', value = '//*[@id="js-vin-btn"]')
submit1_element$clickElement()
Sys.sleep(10)

Solution

  • Perhaps a better and lighter approach (rselenium is my enemy):

    library(tidyverse)
    library(rvest)
    
    session <- "https://checktoprotect.org/vin-check/" %>% 
      session()
    
    result <- session %>% 
      html_form() %>% 
      pluck(1) %>% 
      html_form_set(state_id = "MI", 
                    plate_number = "ELX1701") %>% 
      html_form_submit() %>% 
      read_html() %>% 
      html_elements("strong , .recalls__no-results p , .clear , .recalls__model") %>% 
      html_text()
    
    > result
    [1] "0 Open Recalls for:"                                                                                    
    [2] "2015 GMC SIERRA 1500"                                                                                   
    [3] "1GTR1TEH8FZ298242"                                                                                      
    [4] "MI - ELX1701"                                                                                           
    [5] "New recalls are issued regularly. We recommend checking every 3 months for new recalls on your vehicle."