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.
We can scroll the page up or down in Selenium webdriver using Java. This is achieved with the help of the Actions class. First of all, we have to create an object of this Actions class and then apply the sendKeys method to it. Now, to scroll down a page, we have to pass the parameter Keys.
First, we mentioned that Selenium is a tool for browser automation and it can perform many actions, including scrolling a webpage.
In Selenium we can do this with scrollIntoView(true) which can scroll automatically till the specific element is not present. You might have a requirement to scroll down the page but this requirement was different, so here I used Java Script executor, which allowed me to scroll into view.
Scenario 1: To scroll down the web page by pixel. Javascript method ScrollBy() scrolls the web page to the specific number of pixels. The syntax of ScrollBy() methods is : executeScript("window. scrollBy(x-pixels,y-pixels)");
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With