Search code examples
rgoogle-chromeseleniumadblockrselenium

Enable Adblocker extension In Chrome using latest version of RSelenium


My question is related to this previous one, as I'd similarly like to enable AdBlocker when driving Chrome using the latest version of RSelenium (1.7.1). How do you set the Chrome profile in RSelenium now that startServer has been deprecated?

The code I'm using is as follows, but I don't think the last line is correct. At least, the AdBlocker doesn't appear to be working when Chrome is opened using RSelenium. Thoughts?

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

Solution

  • You can pass the extraCapabilities argument to the rsDriver function:

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

    UPDATE

    You can also add extensions by base 64 encoding the relevant crx file. You can get the chrome crx file from for example 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/ for this:

    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
    

    If you wish to encode with jsonlite:

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

    On MAC OSx sierra:

    I created a profile by adding a person on chrome "seltestprof". The profile is created in folder which can be found by browsing to chrome://version whilst using the profile. You can see the profile path listed here. Mine was

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

    I ran the following code to use the profile:

    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)
    

    To use a base encoded crx I used the following code: library(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
    

    enter image description here