问题描述
我正在运行此代码,没有问题:
library(RSelenium)
library(rvest)
library(tidyverse)
rd <- rsDriver(verbose = FALSE,browser = "chrome",chromever = "85.0.4183.83")
remDr <- rd$client
remDr$navigate("https://recruitcrm.io/jobs/Intellecta?1589280608414")
webElm <- remDr$findElement(using = "class","card-content")
page_source <- remDr$getPageSource()
read_html(page_source[[1]]) %>% html_nodes(".job-list-buttons") %>% html_text() %>% length()
当我将此代码包装到函数中,然后使用purrr中的pmap遍历URL,元素和html节点(URL:{https://recruitcrm.io/jobs/Intellecta?1589280608414,元素:“ card-content”,nodes:“)的列表时。 job-list-button“),出现此错误:
Selenium message:no such element: Unable to locate element: {"method":"css selector","selector":".card\-content"}
我几乎没有其他可以映射的网站。
修改
这是我收到错误消息时运行的代码:
library(RSelenium)
library(rvest)
library(lubridate)
library(tidyverse)
scrape_selenium <- function(url,selector,click_action,heiti_vefsidu,scroll,click) {
rd <- rsDriver(verbose = FALSE,chromever = "85.0.4183.83")
if (scroll) {
remDr <- rd$client
remDr$navigate(url)
for(i in 1:20) {
remDr$executeScript(paste("scroll(0,",i*10000,");"))
Sys.sleep(1)
}
page_source <- remDr$getPageSource()
} else if (click) {
remDr <- rd$client
remDr$navigate(url)
webElm <- remDr$findElement(using = "class",click_action)
for(i in 1:10) {
webElm$clickElement()
Sys.sleep(5)
}
page_source <- remDr$getPageSource()
} else {
remDr <- rd$client
remDr$navigate(url)
webElm <- remDr$findElement(using = "class",click_action)
page_source <- remDr$getPageSource()
}
# 3.0 CREATE AND UPDATE DATA ----
date_today <- today()
fjoldi_auglysinga <- html(page_source[[1]]) %>% html_nodes(selector) %>% html_text() %>% length()
data_tbl <- tibble(date = date_today,fjoldi = fjoldi_auglysinga,vefsida = heiti_vefsidu)
# 4.0 Close broswer and stop the server ----
remDr$close()
rd$server$stop()
gc()
## Stop didn't work so I found this solution:
# https://github.com/ropensci/RSelenium/issues/228
system("taskkill /im java.exe /f",intern=FALSE,ignore.stdout=FALSE)
# 5.0 RETURN
return(data_tbl)
}
url_list <- list(
"https://kopavogur.alfred.is/","https://atvinna.frettabladid.is/storf/","https://www.alfred.is/starfsgrein/laus-storf","https://recruitcrm.io/jobs/Intellecta?1589280608414"
)
selector_list <- list(
"h5",".hentry a","h5",".job-list-buttons"
)
click_list <- list(
NULL,"load_more_jobs",NULL,"card-content"
)
heit_vefsidu <- list(
"Kopavogur","job_is","Alfred","Intellecta"
)
scroll <- list(
TRUE,FALSE,TRUE,FALSE
)
click <- list(
FALSE,FALSE
)
pmap(list(url_list,selector_list,click_list,heit_vefsidu,click),scrape_selenium)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)