Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use RSelenium with already open browser #262

Open
jscottp99 opened this issue Sep 12, 2022 · 3 comments
Open

use RSelenium with already open browser #262

jscottp99 opened this issue Sep 12, 2022 · 3 comments

Comments

@jscottp99
Copy link

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.

@mlane3
Copy link

mlane3 commented Sep 16, 2022

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.

@jscottp99
Copy link
Author

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

@mlane3
Copy link

mlane3 commented Sep 20, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants