Selenium Course
- 62k Enrolled Learners
- Weekend/Weekday
- Live Class
WebDriver is the foundational tool for building browser automation scripts. Along with WebDriver, a few other Selenium interfaces are SearchContext, Remote WebDriver, TakesScreenshot, and JavaScriptExecutor. This blog will discuss different scrolling methods in Selenium WebDriver using JavaScriptExecutor. We will understand how to scroll down in Selenium, scroll by some pixels, scroll until the element is in view, scroll to the bottom of the webpage, and scroll horizontally.
Do you know how to scroll down in Selenium? Scrolling in Selenium is the movement of the cursor on a webpage. This is done to make the areas likely out of reach of the browser window to be seen. Because traditional mouse or keyboard motions are inapplicable in Selenium WebDriver automation scenarios, we employ JavaScriptExecutor to navigate up and down through the content or web pages.
Let us discuss a few scenarios to understand the importance of the scroll down on a webpage.
Integrating the org.openqa.selenium.JavascriptExecutor package in your code is the first step towards implementing scroll down in Selenium, particularly to an element and other pertinent activities.
The JavaScriptExecutor for Selenium syntax, which is necessary to integrate scroll down in Selenium features and other related actions, is shown below:
The JavascriptExecutor interface is used by Selenium automation to run JavaScript code, which is a language that can communicate with HTML in a browser.
How to scroll down in Selenium using actions class? To learn how to scroll to a specific place on the page by entering its exact location or coordinates, refer to the script below to perform the Selenium scroll-down action on the Firefox web browser.
import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class HandleScroll { @Test public void scrollDown() { System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.navigate().to("Website URL"); //to perform Scroll on application using Selenium JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.scrollBy(0,350)", ""); } }
Output: The Geckodriver of Firefox is initialized by the code. After that, the Firefox browser is opened and sent to the given website URL. The browser window scrolls down 350 pixels vertically the website loads.
To scroll up, a user only needs to change the second parameter’s pixel value (350 in this case) to a negative value (-350).
Please refer to the script that follows:
import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class HandleScroll { @Test public void scrollDown() { System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.navigate().to("Website URL"); // Specify the website URL //to perform Scroll on application using Selenium JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.scrollBy(0,-350)", ""); } }
How to use Selenium to scroll down to an element and make it visible
See the Selenium script that follows.
import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class ScrollByVisibleElement { WebDriver driver; @Test public void ByVisibleElement() { System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe"); WebDriver driver = new FirefoxDriver(); JavascriptExecutor js = (JavascriptExecutor) driver; //Launch the application driver.get("https://www.browserstack.com/guide/selenium-scroll-tutorial"); //Locating element by link text and store in variable "Element" WebElement Element = driver.findElement(By.linkText("Try Selenium Testing For Free")); // Scrolling down the page till the element is found js.executeScript("arguments[0].scrollIntoView();", Element); } }
Output: When the above code runs, Firefox will open and the specified URL (the Selenium tutorial) will be navigated to.
Want to learn more about it? Check out the Selenium Course online today.
To accomplish the Selenium scroll-down action on the Firefox browser, refer to the script below.
import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class HandleScroll { @Test public void scrollDown() { System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.navigate().to("Website URL"); //to perform Scroll on application using Selenium JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.scrollBy(0,350)", ""); } }
Output: The Geckodriver of Firefox is initialized by the code. After that, the Firefox browser is opened and sent to the given website URL. The browser window scrolls down 350 pixels vertically as the website loads.
To scroll up, a user only needs to change the second parameter’s pixel value (350 in this case) to a negative value (-350).
Please refer to the script that follows:
import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class HandleScroll { @Test public void scrollDown() { System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.navigate().to("Website URL"); // Specify the website URL //to perform Scroll on application using Selenium JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.scrollBy(0,-350)", ""); } }
How to scroll down in selenium to an element and make it visible has the script written below:
import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class ScrollByVisibleElement { WebDriver driver; @Test public void ByVisibleElement() { System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe"); WebDriver driver = new FirefoxDriver(); JavascriptExecutor js = (JavascriptExecutor) driver; //Launch the application driver.get("https://www.browserstack.com/guide/selenium-scroll-tutorial"); //Locating element by link text and store in variable "Element" WebElement Element = driver.findElement(By.linkText("Try Selenium Testing For Free")); // Scrolling down the page till the element is found js.executeScript("arguments[0].scrollIntoView();", Element); } }
Output: When the aforementioned code runs, Firefox will open and the specified URL (the Selenium tutorial) will be navigated to.
How to scroll down in selenium is a commonly asked question while surfing a webpage. We shall ascertain the page’s height to accomplish a seamless scroll down in Selenium WebDriver. To get to the bottom of the page in Selenium, we shall scroll down after determining the page height. In Selenium automation testing, this technique is commonly employed because scrolling is necessary to perform pertinent actions on the WebElements within the Document Object Model (DOM).
Use the below Selenium script to understand how to scroll down to the bottom of the webpage.
import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class HandleScroll { @Test public void scrollDown() { System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.navigate().to("Website URL"); // Specify the Website URL //to perform scroll on an application using Selenium JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.scrollBy(0,document.body.scrollHeight)"); } }
Output: The aforementioned code will retrieve the webpage’s maximum height from the Document Object Model, after which the scrollBy() function will scroll to the bottom.
One of the Selenium interview questions commonly asked is how to scroll horizontally on a webpage to a specific web element using Selenium. There are situations in which horizontal page scrolling becomes essential while doing automated testing for websites or online applications. Especially when concentrating on horizontal scrolling, it could become necessary to scroll left or right on the page.
Use the below script to scroll horizontally on a webpage to a specific web element using Selenium:
import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class HandleScroll { @Test public void ScrollHorizontally() { System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe"); WebDriver driver = new FirefoxDriver(); JavascriptExecutor js = (JavascriptExecutor) driver; // Launch the application driver.get(" Website URL “); // Specify the website URL WebElement Element = driver.findElement(By.linkText("Auto Testing")); //This will scroll the page Horizontally till the element is found js.executeScript("arguments[0].scrollIntoView();", Element); } }
Output: The aforementioned code launches Firefox and directs users to the given URL. After loading the webpage, Selenium automatically identifies the designated element and scrolls horizontally until it fills the browser window.
Scrollbar handling is critical when automating the test cases involving hidden elements: elements within a scrolling container, elements within an infinite scrolling region, or elements positioned off-screen. With the help of JavaScriptExecutor, the testers can work with scrolling both in the vertical and the horizontal plane. This will help in the practical testing of web and integrated applications without problems with the interaction of elements and dynamic content.
In Selenium, scrolling down works by using sendKeys(Keys. PAGE_DOWN) on the body or any web element. This one outputs the “Page Down” key to drive the cursor down the selected webpage.
To scroll down the screen, you can using JavaScriptExecutor and the script like window. or using ScrollDown to (0,pixels) or using SendKeys.SendWait(“{PgDn}”). These, scrolls down the viewport by a certain number of pixels or simply by the press of the “pagination down” key.
While scrolling to an element in Selenium, there is a concept of JavaScriptExecutor, where command arguments[0] are passed. To achieve the scroll into view, we have to use scrollIntoView(true) on the desired web element. This makes the element appear in the viewport, enabling users to interact with it at some point.
To scroll to the bottom of the page, we need to use the JavaScriptExecutor concept, where we need to write the script window. scrollTo (0, document. body. scrollHeight). This command takes the user to the bottom of the page when it is executed dynamically.
This way, using JavaScriptExecutor in a loop with a window, you can scroll down multiple times. scrollBy (0, pixels) or by sending many sendKeys (Keys. PAGE_DOWN) to the body or an active element. This simulates continuous scrolling.
Course Name | Date | Details |
---|---|---|
Selenium Course | Class Starts on 23rd November,2024 23rd November SAT&SUN (Weekend Batch) | View Details |
Selenium Course | Class Starts on 25th November,2024 25th November MON-FRI (Weekday Batch) | View Details |
Selenium Course | Class Starts on 21st December,2024 21st December SAT&SUN (Weekend Batch) | View Details |
edureka.co