RSelenium : problem with navigate method within a function

I can’t get the navigate method of the client object of rsDriver to work inside a function.
My sandbox is the Quotes to scrape website: http://quotes.toscrape.com/

My libraries and global variables are :

library(RSelenium)
library(httr)

rD <- rsDriver(port = 4442L, browser="firefox", chromever = NULL)
remDr <- rD[["client"]]

quotes <- list()
authors <- list()

and I launch my script with :

get_quotes(start_url)

Here’s my main function get_quotes(), which takes an url as parameter.

start_url <- "https://quotes.toscrape.com/"
remDr$navigate(start_url)
get_quotes <- function(page_url) {
  print(paste("Processing  ", page_url))
  # current page
  new_quotes <- lapply(get_quotes_elements(page_url), get_quote)
  quotes <<- append(quotes, new_quotes)
  # Find next page
  next_quotes_page_url <- get_next_quotes_page_url()
  if (!is.null(next_quotes_page_url)) {
    print(paste("next page url =  ", next_quotes_page_url))
    remDr$navigate(next_quotes_page_url)
    get_quotes(next_quotes_page_url)
  }
}

I use a lapply loop to extract quotes: for each HTML element of class .quote found on the page with the function get_quotes_elements()

get_quotes_elements <- function(page_url) {
  remDr$navigate(page_url)
  quotes_elements <- remDr$findElements("css selector", ".quote")
  return(quotes_elements)
}

, I execute the get_quote() function.

get_quote <- function(quote_element) {
  quote <- list()
  quote_text <- quote_element$findChildElement("css selector", ".text")$getElementText()
  quote_author <- quote_element$findChildElement("css selector", ".author")$getElementText()
  quote_tags_elements <- quote_element$findChildElements("css selector", ".tag")
  quote_tags <- list(unlist(lapply(quote_tags_elements, function(quote_tag){
    quote_tag$getElementText()
  })))
  quote['author'] <- quote_author
  quote['quote'] <- quote_text
  quote['tags'] <- quote_tags
  
  author_page_url <- quote_element$findChildElement("css selector", "a[href*='/author/']")$getElementAttribute("href")
  print(paste("  author url found :", author_page_url))
  remDr$navigate(author_page_url)
  author <- get_author(author_page_url)
  
  return(quote)
}

Then I search for other quotes pages using get_next_quotes_page_url()

get_next_quotes_page_url <- function() {
  next_button_element <- remDr$findElements("css selector", ".next a")
  next_quotes_page_url <- NULL
  if(length(next_button_element) > 0) {
    next_quotes_page_url <- unlist(lapply(next_button_element, function(next_page){
      next_page$getElementAttribute("href")
    }))
  }
  return(next_quotes_page_url)
}

, if so, I call the navigate() method, then execute get_quotes(), and so on for as long as there is a next page quotation link.

Everything works perfectly if I limit the script to extract quotes.
The problem arises when, inside get_quote(), I retrieve the link to the quote author’s description and try to extract the author’s information with get_author function :

get_author <- function(author_page_url) {
  print(paste("  processing scraping of : ", author_page_url))
  author_name = remDr$findElement("css selector", ".author-title")$getElementText()
  print(paste("  author name  ", author_name))
}

The navigation to the author’s page doesn’t work, which raises an Unable to locate element: .author-title error, which is consistent since I’m not on the right page.

Thanks for your help!

Translated with DeepL.com (free version)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật