使用最新版本的 RSelenium 在 Chrome 中启用 Adblocker 扩展

Enable Adblocker extension In Chrome using latest version of RSelenium

我的问题与 this previous one 有关,因为我同样希望在使用最新版本的 RSelenium (1.7.1) 驾驶 Chrome 时启用 AdBlocker。既然 startServer 已被弃用,您如何在 RSelenium 中设置 Chrome 配置文件?

我使用的代码如下,但我认为最后一行不正确。至少,当使用 RSelenium 打开 Chrome 时,AdBlocker 似乎没有工作。想法?

rD <- rsDriver(verbose = F)
remDr <- rD$client
cprof <- getChromeProfile("/Users/<username>/Library/Application Support/Google/Chrome", "Default")
remDr$extraCapabilities <- cprof

您可以将 extraCapabilities 参数传递给 rsDriver 函数:

cprof <- getChromeProfile("/Users/<username>/Library/Application Support/Google/Chrome", "Default")
rD <- rsDriver(verbose = F, extraCapabilities = cprof)
remDr <- rD$client

更新

您也可以通过base 64编码相关的crx文件来添加扩展。例如,您可以从 http://chrome-extension-downloader.com/ (the id currently for adguard adblocker is: bgnkhhnnamicmpeenaelnjfhikgbkllg) once you have the crx file you will need to base64 encode it. I used https://cran.r-project.org/web/packages/base64enc/ 获取 chrome crx 文件:

library(RSelenium)

cprof <- list(chromeOptions = 
                list(extensions = 
                       list(base64enc::base64encode("C:/Users/john/Downloads/Adguard-AdBlocker_v2.5.11.crx"))
                ))
rD <- rsDriver(verbose = F, extraCapabilities = cprof)
remDr <- rD$client

如果你想用jsonlite编码:

tmpfile <- "C:/Users/john/Downloads/Adguard-AdBlocker_v2.5.11.crx"
jsonlite::base64_enc(readBin(tmpfile, "raw", file.info(tmpfile)$size))

在 MAC OSx 塞拉利昂:

我通过在 chrome "seltestprof" 上添加一个人创建了个人资料。配置文件是在文件夹中创建的,可以在使用配置文件时浏览到 chrome://version 找到该文件夹​​。您可以看到此处列出的配置文件路径。我的是

/private/var/folders/c2/d97mz0250bg08rr4g2znxk7m0000gq/T/.org.chromium.Chromium.mS5SA1/Profile 1

我运行以下代码使用配置文件:

library(RSelenium)

cprof <- getChromeProfile("/private/var/folders/c2/d97mz0250bg08rr4g2znxk7m0000gq/T/.org.chromium.Chromium.mS5SA1/", "Profile 1")
rD <- rsDriver(verbose = F, extraCapabilities = cprof)
remDr <- rD$client
remDr$navigate("http://ads-blocker.com/testing/")
remDr$screenshot(display = TRUE)

为了使用基本编码的 crx,我使用了以下代码: 库(RSelenium)

cprof <- list(
  chromeOptions = 
    list(extensions = 
           list(base64enc::base64encode("/Users/admin/Downloads/Adguard-AdBlocker_v2.5.11.crx"))
    )
)
rD <- rsDriver(verbose = F, extraCapabilities = cprof)
remDr <- rD$client