ropensci / RSelenium

An R client for Selenium Remote WebDriver

Home Page:https://docs.ropensci.org/RSelenium

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

use RSelenium with already open browser

jscottp99 opened this issue · comments

Is it possible to attach an RSelenium session to an already open browser? I have found instructions on how to do this in python by opening a Chrome debugger browser session and referencing that session in the extraCapabilities. I'm curious if this is possible in RSelenium.

I believe you can in theory. Could you paste the python instructions that you found @jscottp99?

Digging into the code... and a couple of links....

All extraCapabilities does is pass a profile for chrome or options for other browsers to the Selenium API and to the browser's web driver (like chrome.exe). Currently, I believe R selenium uses Selenium version 2 or 3. So anything in the extraCapabilities should be in there. However, the problem is that R is not on the current version of Selenium so many things you would need to set in both the profile and options. You will need a profile as the StackOverflow posts suggest.

In terms of the options, I believe that this might work...

get_free_port<- function(test_ports=seq(4400, 4450, 1)) {
  used_ports_df <- data.table::fread("netstat -aon -p tcp", skip = 1, header = FALSE)
  used_ports <- as.numeric(sub(".*:", "", used_ports_df$V2))
  free_ports <- setdiff(test_ports, used_ports)
  return(as.integer(free_ports[1]))
}
chrome_ops <- list(
  chromeOptions = 
    list(
    args = c('remote-debugging-port=9014','--user-data-dir="<AnyDirectoryOfYourChoice>"')
    prefs = list("profile.default_content_settings.popups" = 0L)
    )
)
driver <- rsDriver(chromever = config2$drive, browser = "chrome", extraCapabilities = chrome_ops)

If I could see the python tutorial @jscottp99 then I could try to translate it better.

Here is a link to one of the tutorials. This is the example code posted for python:

PYTHON Example

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
#Change chrome driver path accordingly
chrome_driver = "C:\chromedriver.exe"
driver = webdriver.Chrome(chrome_driver, chrome_options=chrome_options)
print driver.title

Ok so then its one of two things:

chrome_ops <- list(
  chromeOptions = 
    list(
    args = c('remote-debugging-port=9014','user-data-dir="<AnyDirectoryOfYourChoice>"')
    prefs = list("profile.default_content_settings.popups" = 0L)
    )
)

Or it is probably...

chrome_ops <- list(
  chromeOptions = 
    list(
    args = c(debuggerAddress="127.0.0.1:9222")
    prefs = list("profile.default_content_settings.popups" = 0L)
    )
)

However, I think you might have to test it out. Let me know how it goes