Kim
Kim

Reputation: 4328

Zoom out of website when using RSelenium without changing page size/resolution

I'd like to zoom out of RSelenium remote, but this is surprisingly difficult to find a solution. I am aware of

library(RSelenium)
driver <- rsDriver()
remDr <- driver[["client"]]
remDr$navigate("https://www.google.com/")
webElem <- remDr$findElement("css", "html")
webElem$sendKeysToElement(list(key = "control", "-"))              ## Does not work
webElem$sendKeysToElement(list(key = "control", key = "subtract")) ## Does not work

The browser is Chrome.

Upvotes: 3

Views: 888

Answers (2)

Allan Cameron
Allan Cameron

Reputation: 174448

I don't think the problem is with sending the keys to the browser, since as noted in the linked posts, it is possible to send control + a to the browser window to select elements. It seems rather that the keys are not being recognised as commands to the browser application.

There are other ways round this problem however.

As @Muzzamil suggests, you can get a similar effect with changing the css of the document body using Chrome, though this doesn't work in Firefox.

If you want to natively change the browser zoom in a way that persists throughout the session, I can demonstrate solutions using Firefox and Chrome, since in both cases one can navigate to the html-based options page and interact with it to set the browser zoom level.

Here's how you do it with Firefox:

library(RSelenium)

zoom_firefox <- function(client, percent)
{
  store_page <- client$getCurrentUrl()[[1]]
  client$navigate("about:preferences")
  webElem <- client$findElement("css", "#defaultZoom")
  webElem$clickElement()
  webElem$sendKeysToElement(list(as.character(percent)))
  webElem$sendKeysToElement(list(key = "return"))
  client$navigate(store_page)
}

This allows the following:

driver <- rsDriver(browser = "firefox")
client <- driver$client
client$navigate("https://www.google.com")
client$screenshot(display = TRUE)

We can see the default zoom is set (100%):

enter image description here

Now we zoom out to 50% like this:

zoom_firefox(client, 50)
client$screenshot(display = TRUE)

enter image description here And zoom back in like this:

zoom_firefox(client, 100)
client$screenshot(display = TRUE)

enter image description here

It's harder with Chrome because its options page uses a complex, nested shadow DOM. Since we can't get an xpath or css selectors to navigate a shadow dom, we need to extract the element's webdriver id using javascript and then force this Id onto another web element which we can then control.

zoom_chrome <- function(client, percent)
{
  store_page <- client$getCurrentUrl()[[1]]
  client$navigate("chrome://settings/")
  webElemId <- client$executeScript(paste0("return document.querySelector",
                                           "(\"body > settings-ui\").",
                                           "shadowRoot.querySelector(\"#main\")",
                                           ".shadowRoot.querySelector",
                                           "(\"settings-basic-page\")",
                                           ".shadowRoot.querySelector",
                                           "(\"#basicPage > ",
                                           "settings-section:nth-child(8)",
                                           "> settings-appearance-page\")",
                                           ".shadowRoot.querySelector",
                                           "(\"#zoomLevel\");"),
                                    args = list("dummy"))
  webElem <- client$findElement("css", "html")
  [email protected]$elementId <- as.character(webElemId)
  webElem$clickElement()
  webElem$sendKeysToElement(list("3"))
  zooms <- c(25, 33, 50, 67, 75, 8:11 * 10, 125, 150, 175, 200, 250, 3:5 * 100)
  desired_zoom <- which.min(abs(percent - zooms))
  current_zoom <- which(zooms == 300)
  n_keys <- desired_zoom - current_zoom
  if(n_keys > 0) 
    for(i in seq(n_keys)) 
      webElem$sendKeysToElement(list(key = "down_arrow"))
  if(n_keys < 0) 
    for(i in seq(abs(n_keys))) 
      webElem$sendKeysToElement(list(key = "up_arrow")) 
  webElem$sendKeysToElement(list(as.character(percent)))
  webElem$sendKeysToElement(list(key = "return"))
  client$navigate(store_page)
}

But it works in the same way:

driver <- rsDriver(browser = "chrome", chromever = "80.0.3987.106")
client <- driver$client
client$navigate("https://www.google.com")
client$screenshot(display = TRUE)
zoom_chrome(client, 50)
client$screenshot(display = TRUE)
zoom_chrome(client, 100)
client$screenshot(display = TRUE)

Which gives exactly the same results as firefox.

Of course, you could easily write a simple wrapper function that selects which "zoom" function to call based on the current browser.

I have not looked into implementing this in internet explorer or phantomjs since they do not have html-based options pages.

Upvotes: 5

Muzzamil
Muzzamil

Reputation: 2881

You can try Zoom out with Java script. Please try below code for zoom out at 90%.

library(RSelenium)
driver <- rsDriver()
remDr <- driver[["client"]]
remDr$navigate("https://www.google.com/")
webElem <- remDr$findElement("css", "html")
script <- "document.body.style.zoom='90%'" 
remDr$executeScript(script, args = list())

Upvotes: 3

Related Questions