我通常试图在R(RSelenium)中复制有关硒的以下问题/答案:
selenium js webdriver - attach to active session
Can Selenium interact with an existing browser session?
在RSelenium中正常启动驱动程序如下:
#LAUNCH INITIAL SERVER
library(wdman)
library(RSelenium)
cDrv <- chrome(version="74.0.3729.6")
eCaps <- list(
chromeOptions = list(
args = c('--no-sandbox','--disable-notifications', '--window-size=1280,800'),
prefs = list(
"enable_do_not_track" = TRUE
)
)
)
remDr<- remoteDriver(browserName = "chrome", port = 4567L, extraCapabilities = eCaps)
remDr$open()
sessionID<-remDr$getSessions()[[1]]$id ###SAVE SESSIONID IF NEEDED
然后,我想保持该Webdriver会话的打开状态,并能够在新的R SESSION中重新连接该会话:
#DIFFERENT R SESSION (same computer or vm instance)
#Made up ideal function (sessionID would be saved from the first script if necessary):
remDr2 <- remoteDriver(port=4567L and/or session=sessionID )
这背后的原因是,我在几个VM上远程使用RSelenium,并且当我执行非常短的硒处理时,启动驱动程序所花费的时间比实际的抓取时间长(因为正在远程查询运行硒的VM仅是一个网页)