R语言中RSelenium连接Chrome失败的问题及可行解决方案
R中RSelenium连接Chrome失败的解决过程
我在学习R语言中使用Selenium时,编写了如下连接代码:
library(RSelenium) library(wdman) library(netstat) library(rvest) selenium_object <- selenium(retcommand = TRUE, check = FALSE) Sys.sleep(3) remote_driver <- rsDriver(browser = "chrome", chromever = "latest", verbose = FALSE, port = free_port(), phantomver = NULL) remDr <- remote_driver$client url <- "https://www.reddit.com/r/PersonalFinanceCanada/comments/1llgjia/is_this_guy_trying_to_scam_me/" remDr$navigate(url) Sys.sleep(5)
运行后出现错误:
Could not open chrome browser. Client error message: Undefined error in httr call. httr output: Failed to connect to localhost port 14415 after 2229 ms: Couldn't connect to server Check server log for further details. Warning message: In rsDriver(browser = "chrome", chromever = "latest", verbose = FALSE, : Could not determine server status.
我的Google Chrome版本:
Chrome is up to date Version 138.0.7204.50 (Official Build) (64-bit)
我手动下载了对应版本的chromedriver并解压,但运行binman::list_versions("chromedriver")仍只显示旧版本:
> binman::list_versions("chromedriver") $win32 [1] "113.0.5672.63" "114.0.5735.16" "114.0.5735.90" "119.0.6045.160"
附上sessionInfo信息:
> sessionInfo() R version 4.4.1 (2024-06-14 ucrt) Platform: x86_64-w64-mingw32/x64 Running under: Windows 11 x64 (build 22631) Matrix products: default locale: [1] LC_COLLATE=English_Canada.utf8 LC_CTYPE=English_Canada.utf8 LC_MONETARY=English_Canada.utf8 LC_NUMERIC=C [5] LC_TIME=English_Canada.utf8 time zone: America/Toronto tzcode source: internal attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] rvest_1.0.4 netstat_0.1.2 wdman_0.2.6 RSelenium_1.7.9 duckdb_1.1.3-2 DBI_1.2.3 loaded via a namespace (and not attached): [1] jsonlite_1.8.8 gtable_0.3.5 dplyr_1.1.4 compiler_4.4.1 Rcpp_1.0.12 tidyselect_1.2.1 xml2_1.3.6 [8] bitops_1.0-9 assertthat_0.2.1 scales_1.3.0 yaml_2.3.8 fastmap_1.2.0 semver_0.2.0 ggplot2_3.5.1 [15] R6_2.5.1 generics_0.1.3 patchwork_1.2.0 curl_5.2.1 knitr_1.49 tibble_3.2.1 munsell_0.5.1 [22] pillar_1.9.0 rlang_1.1.4 utf8_1.2.4 xfun_0.50 caTools_1.18.3 cli_3.6.2 magrittr_2.0.3 [29] ps_1.7.6 processx_3.8.4 digest_0.6.35 grid_4.4.1 rstudioapi_0.16.0 rappdirs_0.3.3 lifecycle_1.0.4 [36] vctrs_0.6.5 evaluate_0.24.0 glue_1.8.0 fansi_1.0.6 colorspace_2.1-0 rmarkdown_2.27 binman_0.1.3 [43] httr_1.4.7 tools_4.4.1 pkgconfig_2.0.3 htmltools_0.5.8.1
最终通过以下两种方法成功解决问题:
方法一:使用chromote包
library(chromote) library(rvest) myfun <- function(n = 5) { b <- ChromoteSession$new() b$Page$navigate("https://www.reddit.com/search/?q=toronto") Sys.sleep(8) html <- b$Runtime$evaluate("document.documentElement.outerHTML")$result$value b$close() urls <- read_html(html) %>% html_elements('a[href*="/comments/"]') %>% html_attr('href') %>% head(n) ifelse(grepl("^/", urls), paste0("https://www.reddit.com", urls), urls) } res <- myfun(5)
方法二:使用selenider包
library(selenider) myfun <- function(n = 5) { session <- selenider_session("chromote") open_url("https://www.reddit.com/search/?q=toronto") Sys.sleep(8) elements <- ss('a[href*="/comments/"]') urls <- sapply(elements, function(elem) { elem_attr(elem, "href") }) urls <- head(urls, n) close_session() ifelse(grepl("^/", urls), paste0("https://www.reddit.com", urls), urls) } res <- myfun(5)
内容的提问来源于stack exchange,提问作者farrow90
相关产品推荐
相关产品推荐

