Scrolling page in RSelenium - r

How can I manually scroll to the bottom (or top) of a page with the RSelenium WebDriver? I have an element that only becomes available when it is visible on the page.

Assuming you got
library(RSelenium)
startServer()
remDr <- remoteDriver()
remDr$open()
remDr$setWindowSize(width = 800, height = 300)
remDr$navigate("https://www.r-project.org/about.html")
You could scroll to the buttom like this:
webElem <- remDr$findElement("css", "body")
webElem$sendKeysToElement(list(key = "end"))
And you could scroll to the top like this:
webElem$sendKeysToElement(list(key = "home"))
And in case you want to scroll down just a bit, use
webElem$sendKeysToElement(list(key = "down_arrow"))
The names of the keys are in selKeys.

Related

spsComps gallery only enlarges once

I got an app that opens up a modalDialog with an image inside a spsComps::gallery. However, the enlargement works only the first time the modal has been opened. How can this be fixed? Here is a minimal reprex:
library(shiny)
library(spsComps)
ui <- fluidPage(
actionButton("modal", "Open modal")
)
server <- function(input, output, session) {
observeEvent(input$modal,
{
showModal(modalDialog(
title = "test",
fluidRow(gallery(
texts = "Click to enlarge", hrefs = "", image_frame_size = 6,
images = "https://cdn.pixabay.com/photo/2018/07/31/22/08/lion-3576045__340.jpg",
enlarge = TRUE, title = "When you close this modal, the enlargement does not work again",
enlarge_method = "modal"
)),
footer = modalButton("Cerrar"),
easyClose = TRUE,
size = "xl"))
})
}
shinyApp(ui, server)
The first time the modal is opened, you can enlarge the image by clicking it. It looks like this:
However, when you close and then reopen the modal, that enlargement feature is missing.
A temporary fix would be this:
library(shiny)
library(spsComps)
ui <- fluidPage(
actionButton("modal", "Open modal"),
singleton(
div(id = "sps-gallery-modal", class = "gallery-modal",
style="display: none;",
onclick = "galModalClose()", tags$span(class = "gallery-modal-close", "X"),
tags$img(id = "sps-gallery-modal-content",
class = "gallery-modal-content"),
div(class = "gallery-caption")
))
)
server <- function(input, output, session) {
observeEvent(input$modal,
{
showModal(modalDialog(
title = "test",
fluidRow(gallery(
texts = "", hrefs = "", image_frame_size = 6,
images = "https://cdn.pixabay.com/photo/2018/07/31/22/08/lion-3576045__340.jpg",
enlarge = TRUE, title = "When you close this modal, the enlargement does not work again",
enlarge_method = "modal"
)),
footer = modalButton("Cerrar"),
easyClose = TRUE,
size = "xl"))
})
}
shinyApp(ui, server)
The reason is I have this singleton(... in the gallery creation function. There is only one enlarge img container needed to be created no matter how many galleries you have (It's not practical to enlarge two pictures at the same time). So enlarged images from different galleries are displayed inside the same enlarge container. This saves computer resources, and singleton in Shiny is the function to prevent duplication. Even if you may call gallery many times, if the content inside singleton is sent to the DOM tree only once, it will not append it again.
The problem is when showModal is closed, Shiny deletes everything inside the modal, including the gallery singleton content. Meanwhile, I think the singleton content validation stays at R level. It does not actually go search the DOM tree if this content exists or not. So Shiny thinks singleton content is there, and therefore refused to send it to DOM when the second time you call showModal.
The fix above append singleton content to fluidPage container instead of Shiny modal container, so when modal is closed, it cannot delete the content.
This is a universal problem in Shiny when you have singleton and modalDialog. There is nothing I can do to fix Shiny, but I may think of a more user-friendly way in the next spsComps version to address it.

Scraping image inside canvas with RSelenium

I'm trying to get the main image on this website
The problem is that the main image is loaded via the HTML canvas tag. When I inspect the source code, the image is loaded in here, with no information on any file:
<canvas id="image1" class="mesImages ui-draggable" style="position: fixed; width: …ransform: rotate(0deg);" title="" tabindex="-1" width="359" height="542">
Is it possible to use RSelenium (and not Python) to get the image in .png or .jpg?
Gathering links with RSelenium
library(tidyverse)
library(RSelenium)
library(netstat)
rD <- rsDriver(browser = "firefox", port = free_port())
remDr <- rD[["client"]]
remDr$navigate("https://archives.landes.fr/arkotheque/visionneuse/visionneuse.php?arko=YTo3OntzOjQ6ImRhdGUiO3M6MTA6IjIwMjItMTAtMTgiO3M6MTA6InR5cGVfZm9uZHMiO3M6MTE6ImFya29fc2VyaWVsIjtzOjQ6InJlZjEiO3M6MToiNCI7czo0OiJyZWYyIjtzOjM6IjE3MyI7czoyMjoiZm9yY2VfbnVtX2ltYWdlX2RlcGFydCI7aTozNDA7czoxNjoidmlzaW9ubmV1c2VfaHRtbCI7YjoxO3M6MjE6InZpc2lvbm5ldXNlX2h0bWxfbW9kZSI7czo0OiJwcm9kIjt9")
accept <- remDr$findElement("xpath", '//*[#id="licence_clic_bouton_accepter"]')
accept$clickElement()
more <- remDr$findElement("css selector", "#arkoVision_barToggleButton_bottom")
more$clickElement()
content <- remDr$getPageSource()[[1]]
links <- content %>%
read_html() %>%
html_elements(".imgThumb_container") %>%
html_children() %>%
html_attr("rel") %>%
.[!is.na(.)]
> links %>% head()
[1] "https://archives.landes.fr/arkotheque/visionneuse/img_prot.php?i=13a087d1f5.jpg"
[2] "https://archives.landes.fr/arkotheque/visionneuse/img_prot.php?i=2159e78416.jpg"
[3] "https://archives.landes.fr/arkotheque/visionneuse/img_prot.php?i=65786a44b1.jpg"
[4] "https://archives.landes.fr/arkotheque/visionneuse/img_prot.php?i=702fbd57fa.jpg"
[5] "https://archives.landes.fr/arkotheque/visionneuse/img_prot.php?i=9c4421d51e.jpg"
[6] "https://archives.landes.fr/arkotheque/visionneuse/img_prot.php?i=a4b68fd913.jpg"

Rselenium org.openqa.selenium.WebDriverException

Using RSelenium, I want to click the
enter image description here
button. this page has two views a desktop mode and a mobile mode,
I think the desktop view is hidden and does not allow you to click. enter image description here.
server <- phantomjs(port=5005L)
remDr <- remoteDriver(browserName = "phantomjs", port=5005L)
remDr$open()
remDr$navigate("https://catalogo.movistar.com.pe/samsung-galaxy-note-10-plus")
remDr$screenshot(display = TRUE)
element<- remDr$findElement(using = 'css selector', "body > div:nth-child(6) > div.container.detail-view > div.detail-main-head-container > div.detail-main-right > div.phone-details-specs > div.phone-specs > div.phone-specs-planes > div > div > div.owl-stage-outer > div > div:nth-child(4)")
element$clickElement()
remDr$screenshot(display = TRUE)
Apparently I must show the hidden view to be able to click because in mobile mode I do not get it.
Your help please. Aquí le tome una foto a mi pantalla con las salidas de la ejecución del código enter image description here

How to add scroll bar and cross button to pop up window in shiny app?

I want to have scroll bar to scroll up and down, cross button to close the pop up window and default of 10 records should display instead of 25 now.
I don't know how to write code for this.
library(shiny)
library(shinydashboard)
library(shinyjs)
library(shinyBS)
data <- iris
ui <- tagList(
useShinyjs(),
dashboardPage(
dashboardHeader(title = "Telemedicine HP"),
dashboardSidebar(),
dashboardBody(
fluidRow(
div(id='clickdiv',
valueBox(60, subtitle = tags$p("Attended", style = "font-
size: 200%;"), icon = icon("trademark"), color = "purple", width = 4,
href
= NULL)
)
)
)
)
)
server <- function(input, output, session){
onclick('clickdiv', showModal(modalDialog(
title = "Your title",
renderDataTable(data)
)))
}
shinyApp(ui, server)
By clicking on valuebox a pop up window will appear showing some tabular data.
But that window should have a scroll bar, cross button in right top corner and records should be shown 10 by default instead of 25 showing now in top left corner of the pop up window.
Can anyone help me with this ?
If your server part is like this, it is limited to 10 shown per page:
server <- function(input, output, session){
onclick('clickdiv', showModal(modalDialog(
title = "Your title",
renderDataTable(data, options = list(
pageLength = 10,
scrollY = "400px"
))
)))
}
I'm not sure I understand the need for the other parts. With 10 records, you don't need to be able to scroll up and down, but even when I set this to a lot of records (say 100), the normal page scroll bar works fine. And there is a button to dismiss the table already (although I appreciate it is not the cross in the corner you are requesting).
You can change other parts of your DataTable using the options - you can see some examples here.
Hope this helps!
EDIT: I've added an option for a vertical scroll bar. You can change the number to suit you.
If that doesn't work then you might be using a setup (for eg Mac) where scrollbars are hidden by default until you start scrolling.

How to add icon in button using library(tcltk) in R?

I use the library (tcltk) in R to make the GUI. Code to create a button is :
tt <- tktoplevel()
button.widget <- tkbutton(tt, text = "", command = function())
I want the existing button on the GUI has an icon. How is the code to add the icon in the button using the library (tcltk) in R?
Use the tkimage.create function to create a Tcl-level representation of an image file. Note that there are some limitations on image formats and I believe GIF is the easiest to work with. Then, you specify the image as the image argument to tkbutton. Here's an example using, an Example.gif from Wikipedia:
library("tcltk")
img <- tclVar()
tclimg <- tkimage.create("photo", img, file = "Example.gif")
tt <- tktoplevel()
button.widget <- tkbutton(tt, text = "Click me!", image = tclimg, compound = "top",
command = function() tkmessageBox(message = "Hello!"))
tkgrid(button.widget)
You can control the relative placement of the image and text on the button using the compound argument:
"none" (display only the image if present, otherwise the text; the default)
"text" (text only)
"image" (image only)
"center" (text in center of image)
"top" (image above text)
"left" (image left of text)
"bottom" (image below text)
"right" (image right of text)

Resources