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

Session inside function #32

Open
markwsac opened this issue Oct 12, 2024 · 1 comment
Open

Session inside function #32

markwsac opened this issue Oct 12, 2024 · 1 comment

Comments

@markwsac
Copy link

Thanks for this great package. Great work!
How can we use session inside a function? The same session opens if I put outside the function...

open_chrome <- function() {
  session <- selenider_session("selenium", browser = "chrome",
                               options = selenium_options(
                                 client_options = selenium_client_options(
                                   capabilities = list(
                                     `goog:chromeOptions` = list(
                                       args = list("--window-size=1920,1080", "--log-level=3",
                                                   "--user-data-dir=C:\\Users\\xxxx\\AppData\\Local\\Google\\Chrome\\User Data",
                                                   "--profile-directory=Default"),
                                       excludeSwitches = list("disable-popup-blocking", "enable-logging")
                                     ))
                                 )
                               ))
  
return(session)  
}

session <- open_chrome()

Error --

Error in `create_selenium_client_internal()`:
! A Selenium session could not be started
Caused by error in `httr2::req_perform()`:
! HTTP 500 Internal Server Error.
✖ Session not created.
✖ Could not start a new session. Error while creating session with the driver service. Stopping driver service: Could not start a new session. Response code 500. Message: Chrome failed to start: exited normally.
    (DevToolsActivePort file doesn't exist)
    (The process started from chrome location C:\Program Files\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.) 
  Host info: host: 'xxxx', ip: 'xxx.xxx.x.x'
  Build info: version: '4.25.0', revision: '030fcf7918'
  System info: os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '22'
  Driver info: driver.version: unknown
  Build info: version: '4.25.0', revision: '030fcf7918'
  System info: os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '22'
  Driver info: driver.version: unknown
@ashbythorpe
Copy link
Owner

By default, selenider_session() creates a session that only lasts as long as the current context, which in your case is the environment inside the open_chrome() function. This means that the session is closed when open_chrome() finishes executing. To change this, use the .env argument.

open_chrome <- function(.env = rlang::caller_env()) {
  selenider_session("selenium",
    browser = "chrome",
    options = selenium_options(
      client_options = selenium_client_options(
        capabilities = list(
          `goog:chromeOptions` = list(
            args = list(
              "--window-size=1920,1080", "--log-level=3",
              "--user-data-dir=C:\\Users\\xxxx\\AppData\\Local\\Google\\Chrome\\User Data",
              "--profile-directory=Default"
            ),
            excludeSwitches = list("disable-popup-blocking", "enable-logging")
          )
        )
      )
    ),
    .env = .env
  )
}

session <- open_chrome()

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