rrvestchromote

"Target position can only be set for new windows" in chromote in R


I'm guessing this is probably some strange confluence of the latest Chrome version and chromote, but since about 24 hours ago, I get "Error in callback(...) : code: -32602 message: Target position can only be set for new windows" whenever I try to create a LiveHTML object when one already exists.

I've tried this on two computers with the same result, which is infuriating.

R 4.4.3, chromote 0.4.0, rvest 1.0.4, Chrome Version 134.0.6998.36

MRE below - beware the shell command!

rm(list = ls())
library(rvest)

shell("taskkill /im chrome.exe /f")
cat("\f")

# This works
x <- read_html_live("https://opencritic.com/")
x$view()

# This does not
y <- read_html_live("https://www.metacritic.com/")

# Nor does this
x$initialize("https://www.metacritic.com/")

Solution

  • Monitoring DevTools protocol message would probably help while debugging this:

    library(chromote)
    s1 <- ChromoteSession$new()
    s1$parent$debug_messages(TRUE)
    s2 <- ChromoteSession$new()
    #> SEND {"method":"Target.createTarget","params":{"url":"about:blank","width":992,"height":1323},"id":5}
    #> RECV {"id":5,"error":{"code":-32602,"message":"Target position can only be set for new windows"}}
    #> Error in callback(...): code: -32602
    #>   message: Target position can only be set for new windows
    chromote_info()
    #> ---- {chromote} ----
    #>    System: x86_64-w64-mingw32
    #> R version: R version 4.4.3 (2025-02-28 ucrt)
    #>  chromote: 0.4.0.9000
    #> 
    #> ---- Chrome ----
    #>    Path: C:\Program Files\Google\Chrome\Application\chrome.exe
    #> Version: 134.0.6998.36
    #>    Args: --headless --disable-gpu --force-color-profile=srgb
    #>          --disable-extensions --mute-audio
    

    So there's something about Target.createTarget parameters. newWindow is indeed false by default and currently there seems to be no way to set it through chromote, but there's this hint:

    (false by default, not supported by headless shell)

    Might switching from Chrome to headless shell be a viable workaround?
    As it happens, dev version of chromote recently gained some experimental support for managing chrome-headless-shell binaries, if installing from GitHub or R-universe is an option, we could update chromote

    install.packages('chromote', repos = c('https://rstudio.r-universe.dev', 'https://cloud.r-project.org'))
    # or
    # remotes::install_github("rstudio/chromote")
    
    sessioninfo::package_info(c("chromote", "rvest"), dependencies = FALSE)
    #>  package  * version    date (UTC) lib source
    #>  chromote   0.4.0.9000 2025-03-05 [1] https://rstudio.r-universe.dev (R 4.4.3)
    #>  rvest      1.0.4      2024-02-12 [1] RSPM
    

    to use local_chrome_version():

    library(rvest)
    chromote::local_chrome_version(binary = "chrome-headless-shell", quiet = FALSE)
    #> chromote will now use version 134.0.6998.35 of `chrome-headless-shell` for
    #> win64.
    
    x <- read_html_live("https://opencritic.com/")
    y <- read_html_live("https://www.metacritic.com/")
    
    x |> html_element("title") |> html_text()
    #> [1] "OpenCritic - Video Game Reviews from the Top Critics in Gaming - OpenCritic"
    y |> html_element("title") |> html_text()
    #> [1] "Movie Reviews, TV Reviews, Game Reviews, and Music Reviews - Metacritic"
    
    x$session$Browser$getVersion() |> str()
    #> List of 5
    #>  $ protocolVersion: chr "1.3"
    #>  $ product        : chr "HeadlessChrome/134.0.6998.35"
    #>  $ revision       : chr "@ea6ef4c2ac15ae95d2cfd65682da62c093415099"
    #>  $ userAgent      : chr "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/134.0.6998.35 Safari/537.36"
    #>  $ jsVersion      : chr "13.4.114.14"
    x$session$parent
    #> <Chromote> (active + alive)
    #>   URL:  http://127.0.0.1:24103
    #>   PID:  26084
    #>   Path: C:/Users/margusl/AppData/Local/R/cache/R/chromote/chrome/134.0.6998.35/chrome-headless-shell-win64/chrome-headless-shell.exe
    

    With current versions we do loose functional $view() when using headless shell, but we can still go through few manual steps to use Chrome DevTools remote debugging
    I.e. open chrome://inspect/#devices and configure it to use location from x$session$parent$url() (127.0.0.1:24103 in this example), it might take few moments before target list gets refreshed.


    With current stable chromote release one could download headless shell manually from https://googlechromelabs.github.io/chrome-for-testing/ and set CHROMOTE_CHROME env.var:

    library(rvest)
    Sys.setenv(CHROMOTE_CHROME = chrome_headless_shell_path)
    x <- read_html_live("https://opencritic.com/")
    

    Relevant changeset in Chromium codebase should be this: https://github.com/chromium/chromium/commit/0144c63992630145a4898a8b1ce2f3cfbca0091d , committed ~2 months ago on Jan 8, affecting releases starting with 134.0.6946.0.