Expose current url?
Closed this issue · 1 comments
Thanks for this package - at the moment, it seems to be a great alternative to rSelenium that often causes me issue.
I am now trying to use chromote
in a context where URLs are often redirected - yet was struggling to find a way to obtain the current URL. After various searches, I realised that I can run driver$Runtime$evaluate("window.location.href")$result$value
... so unless that breaks, my issue is resolved - but maybe still worth simplifying?
I just asked ChatGPT how to do this, and here's what it said:
To get the URL of the current page using the Chrome DevTools Protocol (often referred to as CDP), you'll typically use the
Page
domain and thePage.getNavigationHistory
method. This method returns the navigation history of the current page, from which you can extract the URL of the current page.Here's a step-by-step guide:
Connect to Chrome: Before you can send any commands to Chrome via CDP, you need to establish a connection. This usually involves launching Chrome with remote debugging enabled or connecting to an already running instance.
Enable the
Page
domain: Before you can use methods from thePage
domain, you need to enable it.Call the
Page.getNavigationHistory
method: This method will return the navigation history, including a list of entries. The current entry is the one with the index specified bycurrentIndex
.Extract the URL: From the returned navigation history, you can get the URL of the current page.
I tried it with Chromote:
b <- ChromoteSession$new()
b$Page$navigate("http://github.com")
b$Page$navigate("http://r-project.org")
str(b$Page$getNavigationHistory())
#> List of 2
#> $ currentIndex: int 2
#> $ entries :List of 3
#> ..$ :List of 5
#> .. ..$ id : int 2
#> .. ..$ url : chr "about:blank"
#> .. ..$ userTypedURL : chr "about:blank"
#> .. ..$ title : chr ""
#> .. ..$ transitionType: chr "typed"
#> ..$ :List of 5
#> .. ..$ id : int 4
#> .. ..$ url : chr "https://github.com/"
#> .. ..$ userTypedURL : chr "http://github.com/"
#> .. ..$ title : chr "GitHub: Let’s build from here · GitHub"
#> .. ..$ transitionType: chr "typed"
#> ..$ :List of 5
#> .. ..$ id : int 6
#> .. ..$ url : chr "https://www.r-project.org/"
#> .. ..$ userTypedURL : chr "http://r-project.org/"
#> .. ..$ title : chr "R: The R Project for Statistical Computing"
#> .. ..$ transitionType: chr "typed"
x <- b$Page$getNavigationHistory()
x$entries[[x$currentIndex + 1]]
#> $id
#> [1] 6
#>
#> $url
#> [1] "https://www.r-project.org/"
#>
#> $userTypedURL
#> [1] "http://r-project.org/"
#>
#> $title
#> [1] "R: The R Project for Statistical Computing"
#>
#> $transitionType
#> [1] "typed"