• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Advance Selenium / How to Scroll into view in Selenium Webdriver

How to Scroll into view in Selenium Webdriver

November 10, 2020 by Mukesh Otwani 74 Comments

Scroll into view in Selenium

A few days back when I was automating my application I got one scenario where I had so many scroll bars into the single page itself and I had to scroll all bar one by one to get specific text which I had to validate. It was a little challenging to scroll for each element. In Selenium we can do this with scrollIntoView(true) which can scroll automatically till the specific element is not present.

If you are not aware of how to validate text then the below post will help you How to verify text in Selenium

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.

If you want to scroll down the page itself then you can check the below post which allows us to do the same.

How to scroll down into page using Selenium Webdriver

Scroll into view in Selenium

 

 

How to Scroll into view in Selenium Webdriver.

Now let us talk about how exactly we can scroll this using Selenium. Unfortunately, Selenium does not have an inbuilt method that allows us to scroll into view but it does not mean we cannot scroll.

We can scroll into view in Selenium using JavaScript executor.

You must be wondering now Java Script will help us in this case. I have created a post, which actually tells you what is the need for JavaScript in Selenium, and what is the other thing, which we can do via Java Script.

What is Java Script Executor in Selenium and What are the Usage of Same.

 

 

Recently I have uploaded a video on the same please check.

 

Program for Scroll into view in Selenium Webdriver

Now we will see the detailed program for this we actually scroll into view in Selenium, will extract the text, and will verify the same.

In my case, I am just verifying it but in your case, you may have to perform some other activity as well.

I will be using this application as Sample- Sample Application for Demo

package Demo;

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;



public class TestScroll {

public static void main(String[] args) {

// Start browser

WebDriver driver=new FirefoxDriver();



// Maximize browser

driver.manage().window().maximize();



// Pass application URL

driver.get("http://manos.malihu.gr/repository/custom-scrollbar/demo/examples/complete_examples.html");



// Create instance of Javascript executor

JavascriptExecutor je = (JavascriptExecutor) driver;



//Identify the WebElement which will appear after scrolling down

WebElement element = driver.findElement(By.xpath(".//*[@id='mCSB_3_container']/p[3]"));



// now execute query which actually will scroll until that element is not appeared on page.

je.executeScript("arguments[0].scrollIntoView(true);",element);



// Extract the text and verify

System.out.println(element.getText());

}

}

If you find this useful then please share with friends. Comment below if any doubt, suggestion or feedback.

For More updates Learn Automation page

For any query join Selenium group- Selenium Group

Filed Under: Advance Selenium Tagged With: Scroll Bar in Selenium, Scroll Into view

Reader Interactions

Comments

  1. kunal says

    July 3, 2022 at 7:03 AM

    hello sir i am using method
    js.executeScript(“arguments[0].scrollIntoView(true);”, element);;
    to scroll to the linktext but it is not working

    Reply
    • Mukesh Otwani says

      July 22, 2022 at 1:09 PM

      Please share details about application

      Reply
  2. RANJAN V says

    July 2, 2021 at 8:05 PM

    Nice one)))

    Reply
  3. Pranali says

    May 25, 2020 at 12:16 AM

    How to use srollIntoView() to scroll up the screen?

    Reply
    • Mukesh Otwani says

      May 25, 2020 at 12:13 PM

      Hi Pranjali,

      The same code works for scrolling up. You need to provide a unique element available at top portion of webpage.

      Reply
      • Roja Das says

        June 5, 2023 at 11:07 AM

        Hi. Mukesh ,

        for side bar not able to scroll down . can u please suggest me code

        Reply
        • Mukesh Otwani says

          July 24, 2023 at 12:22 AM

          Hi Roja,
          Can you share the app url so that I can suggest something.

          Reply
  4. Ragesh C says

    April 21, 2020 at 4:20 AM

    Hi Mukesh,

    In my case below 1st statement is failing because the element is not visible at this point. It becomes visible only after scrolling. I need to click the object after scrolling. What should i be doing in that case? i tried scrolling using pixel but it does not scroll. Mine is a react application. Hope to see a response.

    WebElement lastModifiedColHeader = driver.findElement(By.xpath(“//*[@class=’ag-header-row’]//*[contains(text(),’LAST MODIFIED’)]”));
    js.executeScript(“arguments[0].scrollIntoView();”, driver.findElement(By.xpath(“//*[@class=’ag-header-row’]//*[contains(text(),’LAST MODIFIED’)]”)));

    Reply
    • Mukesh Otwani says

      April 22, 2020 at 1:44 PM

      Hi Ragesh,

      Use scrollIntoview instead of using pixel.

      Reply
  5. Jasimuddin shaikh says

    December 27, 2019 at 4:54 PM

    hi Mukesh,

    can you please say for e.g jse.executeScript(“window.scrollBy(1000,0)”, “”);

    how to find pixels .as (1000,0)

    i understand these are for x axis and y axis how to find it by inspecting an element.

    Reply
    • Mukesh Otwani says

      December 30, 2019 at 7:41 PM

      Hi Shaikh,

      Using Point class, you can find X and Y pixels(aka Coordinates)

      Reply
  6. srinivas says

    November 14, 2019 at 5:05 PM

    Hi mukesh regularly i wantched ur videos and following ur content and i have the doubts on sliding bar which is in your videos you told about the main scrolling bars and internal scrolling bars how we perform in how many ways

    Reply
    • Mukesh Otwani says

      November 14, 2019 at 11:51 PM

      Hi Srini, can you give example that what is your main issue?

      Reply
  7. Heena says

    July 17, 2019 at 10:03 AM

    Hi Mukesh,
    I have scroll horizontally to right using Action perform as below:
    Actions actions = new Actions(driver);
    actions.moveToElement(element1).perform();

    Now, I again want to scroll back to original position/left. How I can achieve this. I used below approach:
    Actions actions = new Actions(driver);
    actions.moveToElement(element2).perform();
    OR
    (JavascriptExecutor)driver.executeScript(“arguments[0].scrollIntoView()”, element2);

    Can you please suggest me some better approach to scroll left and right on the same page multiple times

    Reply
    • Mukesh Otwani says

      July 18, 2019 at 11:33 AM

      Hi Heena,

      Have you tried using pixels like this jse.executeScript(“window.scrollBy(1000,0)”, “”);. It uses negative and positive x axis pixels to move.

      Reply
  8. Hariom says

    June 5, 2019 at 1:03 PM

    Hi,

    Is the above script can be used for normal scrolling function in window. Please help….

    Thanks

    Reply
    • Mukesh Otwani says

      June 6, 2019 at 10:49 AM

      Hi Hariom,

      This scroll into view works wherever web ui is available.

      Reply
  9. Smruti says

    May 30, 2019 at 4:30 PM

    Hi,

    I am using JavascriptExecutor for scroll. However, in Selenium 3 onward, the JSExecutor needs the Gecko Driver to run the script. Is there any way without using the gecko driver I can scroll?

    Appreciate your help..

    Reply
    • Mukesh Otwani says

      May 30, 2019 at 7:18 PM

      Hi Smruti,

      There is no connection between Gecko driver and JSExecutor. With Selenium 3, gecko driver is required to launch Firefox browser just like we do for Chrome browser.
      JavaScriptExecutor is as it was…:)

      Reply
  10. Guillermo Chussir says

    April 26, 2019 at 9:33 AM

    Thanks! This really helped me solve a problem I ran into.

    Reply
    • Mukesh Otwani says

      April 26, 2019 at 10:56 AM

      Cheers Guillermo Chussir 🙂 I am glad it helped.

      Reply
  11. kritika aswal says

    April 22, 2019 at 4:31 PM

    i m facing java.lang.NullPointerException
    in line no 51 of this code

    Reply
    • Mukesh Otwani says

      April 23, 2019 at 12:28 AM

      Hi Kritika,

      Code looks fine. Can you share your code?

      Reply
  12. Vi says

    April 12, 2019 at 3:37 PM

    Thanks for the article!

    I have a dynamic list, containing varying no. of items, being displayed based on some selection.
    If the list has more than 7 items, on hovering, the list shows a scroll bar. (If there are fewer items, there will be no scroll bar.)
    I have found the element of the scroll bar based on its class name but tring to scroll using webdriver fails.
    Here is the code:

    //hover
    Actions action = new Actions(driver);
    WebElement placesList = driver.findElement(By.id(“divPlaces”));
    action.moveToElement(placesList).perform();
    //scroll bar element
    element = driver.findElement(By.className(“sbar”));
    //scroll the list
    ((JavascriptExecutor) driver).executeScript(“arguments[0].scrollIntoView(true)”, element);

    Reply
    • Mukesh Otwani says

      April 12, 2019 at 7:41 PM

      Hi Vi,

      If you check my blog post then you might have observed that as per your scenario, (JavascriptExecutor) driver).executeScript(“arguments[0].scrollIntoView(true)”, element); ‘element’ here is Webelement where you want to scroll down the control. So in your scenario you have taken scroll bar itself as webelement(as per my observation based on naming convention) which is wrong. You have to mention element as webelement where you want to reach after scroll action.

      Reply
      • Vi says

        April 15, 2019 at 10:49 AM

        Thanks Mukesh,

        I was expecting scroll bar as web element and hence was trying to get its locator.
        In my case, the scroll bar appears when I hover mouse over the list. The items in the list have hyperlinks and clickable. With the above code (after removing the statement to get scroll bar element), the “frame” containing a drop down box and this list, move up. It doesn’t move up the scroll bar of the list and thus I am not able to view hidden item without scrolling.
        (Please note this is not scrolling the browser’s scroll bar but a dynamic scroll bar in a list of hyperlinked items. If the list has more than 7 items, on hovering, the list shows a scroll bar otherwise no scroll bar.)

        How can I send you a screenshot of the page?

        HTML code:

        Places

        hyd

        (removed 7 more items for simplicity…)

        Reply
      • Vi says

        April 15, 2019 at 11:18 AM

        Update: I got a thought to share. The list element is always visible on the left of the page (and I need not move/scroll to this element to make it visible as actions.moveToElement() does). When I hover, scroll bar within this list appears and I can click the bar, drag it down to see (last) items.

        With the above code, it is moving the “frame” up i.e. everything from ‘Country’ to the list below move a bit up but still the last hidden item in the dynamic is not visible.

        Reply
        • Mukesh Otwani says

          April 15, 2019 at 4:58 PM

          Hi Vi,

          Handling this kind of control using Selenium is tough as data in drop down is dynamic. You have to do scroll action multiple times in order to see full drop down. I can try from if share me app url because this requirement requires hit and try actions.

          Reply
  13. Kunal says

    March 29, 2019 at 7:32 PM

    Hi, can you tell how much time it takes to scroll to the element?
    Do I need to apply thread.sleep(millisecond) after this

    Reply
    • Mukesh Otwani says

      March 29, 2019 at 7:47 PM

      Hi Kunal,

      I can’t assure you about timings because it depends on Web UI. You need to observe scrolling behavior and use appropriate time.

      Reply
  14. Pooja says

    February 17, 2017 at 2:35 PM

    It helped me in scrolling down But using this code, doesn’t scroll up
    Eg. Dropdown values are 1950 to 2044 and When i click on element in UI, 2003 appears on the screen. So, using given javascript executer code, I am able to select values after 2003 and i am failed to select values before 2003. Please help

    Reply
    • Mukesh Otwani says

      February 17, 2017 at 10:04 PM

      Hi Pooja,

      Javascript Executor always scroll the page when it is able to find element on page. In your case, click enables the values to get visible after 2003. Then how driver get values which are before 2003. WebDriver object catches those elements which are visible in developer tool.

      Reply
      • Pooja says

        February 17, 2017 at 10:26 PM

        Okay, i understood. but when i click on element in UI, drop down gets open and only 2003 to 2009 values are visible But still i am able to select upto 2044 using scrollIntoview even those are not visible. then why not before 2003.

        Is there any other way to select values before 2003?

        Reply
        • Mukesh Otwani says

          February 18, 2017 at 7:20 PM

          Hi Pooja,

          Check whether you are able to find elements from Developer tool which are coming before 2003.

          Reply
  15. Sudha says

    February 7, 2017 at 9:27 PM

    Hi Mukesh,
    I’m new to automation. your tutorials are really very helpful. Keep rocking!! Thank you so much

    Reply
    • Mukesh Otwani says

      February 9, 2017 at 7:06 PM

      Hi Sudha,

      Thanks for you positive comments.

      Reply
  16. Kishan says

    February 3, 2017 at 1:56 AM

    Hi Mukesh,

    Thanks for all the efforts and help you provide in teaching us selenium. I have a doubt in below code:

    je.executeScript(“arguments[0].scrollIntoView(true);”,element);

    i want to know what does ‘arguments[0]’ mean?

    Thanks in advance!!

    Reply
    • Mukesh Otwani says

      February 3, 2017 at 5:57 PM

      Hi Kishan,

      arguments[0] defines the index of locator on the page. Suppose for given locator, if you are having multiple matching nodes then arguments[0] points to first one.

      Reply
  17. anjali says

    December 28, 2016 at 11:07 AM

    hi,mukesh …this code is not working for me ,i have to select time from the dropdown,for which i need to scroll down ,up to the element .Can u pls provide me the code ,as I am working on the company’s project and i need it asap

    Reply
    • Mukesh Otwani says

      December 28, 2016 at 11:12 AM

      Hi Anjali,

      This code working for me. Try to get unique element then above code will work.

      Reply
    • anjali says

      December 28, 2016 at 11:18 AM

      i provided the x path of the element ,upto which i have to scroll,but it is not working ,but when iam using movetoElement,it is working fine ,but basically the problem is ,i have to select the time from the excel and then i have to compare it with the element present on the main page ,,for that i have to scroll it down,

      Reply
      • Mukesh Otwani says

        December 28, 2016 at 11:36 AM

        So you can do one thing also you can scroll full page then you can find that element as well.

        Reply
  18. SHWETABH SRIVASTAVA says

    December 3, 2016 at 8:15 PM

    Thanks Mukesh ..Its working for me

    Reply
    • Mukesh Otwani says

      December 6, 2016 at 11:57 AM

      Great Shwetabh

      Reply
  19. Shraddha Gautam says

    November 9, 2016 at 1:53 PM

    Hi Mukesh,
    I am working on new site, and as you know News block are dynamic, some time they appears in top and another time they can come down or removed from the page. can you please suggest how to scroll in this kind of page where you don’t know the exact location and you have to move down to find the element also wait statement is needed in it. please help me out.

    Reply
    • Mukesh Otwani says

      January 11, 2017 at 3:03 PM

      is this fixed?

      Reply
  20. Venkatesh says

    October 21, 2016 at 2:09 PM

    Hi Mukesh Sir,
    How to scrool windows application popup

    Reply
    • Mukesh Otwani says

      October 25, 2016 at 5:44 PM

      Hi Venkatesh,

      Selenium mainly automate browsers.

      Reply
  21. roja says

    August 22, 2016 at 6:38 PM

    hi sir,i want scroll down to bottom …if i use scrollToView() method shall use WebElement…
    without using WebElement how can i use this methos like
    public void clickgooglePlusIcon()
    {
    je.executeScript(“arguments[0].scrollIntoView(true);”);
    //je.executeScript(“window.scrollTo(0, document.body.scrollHeight)”);
    WebDriverWait wait = new WebDriverWait(getDriver(), 20);
    je.executeScript(“arguments[0].scrollIntoView(true);”);
    //je.executeScript(“window.scrollTo(0, document.body.scrollHeight)”);

    googlePlus.click();

    }
    pls give me the solution

    Reply
    • Mukesh Otwani says

      August 27, 2016 at 11:22 PM

      Is this code is working?

      Reply
  22. Ravi says

    August 3, 2016 at 1:57 AM

    Hi Mukesh ,

    Can you help in providinng a sloution to perform swipe action on an web element in Web View.

    Reply
    • Mukesh Otwani says

      August 5, 2016 at 12:13 PM

      Hi Ravi,

      Will upload article on this.

      Reply
  23. Bala says

    June 22, 2016 at 8:43 PM

    Very good.. simple and easy to understand… Thanks!

    Reply
    • Mukesh Otwani says

      July 2, 2016 at 3:22 PM

      Thanks Bala 🙂

      Reply
  24. Mohammed Shahab Ahmed says

    May 25, 2016 at 8:09 PM

    Thanks a lot. Your way of explanation is very good. I was searching such type of videos from very long time, now i got . Thanks again and keep it up.

    Reply
    • Mukesh Otwani says

      May 30, 2016 at 12:40 AM

      Hey Ahmed,

      I am glad to know that it helped you 🙂 Keep visiting and let me know if any help required from my side.

      Reply
  25. Abhishek Singh says

    April 19, 2016 at 4:24 PM

    Thax Mukesh….

    Reply
    • Mukesh Otwani says

      April 20, 2016 at 12:10 PM

      Thanks Abhishek

      Reply
  26. Rasta Jeff says

    April 13, 2016 at 11:17 PM

    thank you so much, it worked

    Reply
    • Mukesh Otwani says

      April 14, 2016 at 2:08 PM

      Cheers Rasta

      Reply
  27. seleniumkoteswarao says

    April 10, 2016 at 1:28 AM

    mukesh.. it is not working for inner tables,.. provide any other alternate sol.

    Reply
    • Mukesh Otwani says

      April 10, 2016 at 11:32 AM

      Hi Koteswarao,

      Please share the sample application because its working from my end.

      Reply
  28. Ann says

    February 19, 2016 at 4:53 AM

    This helped me!

    Reply
    • Mukesh Otwani says

      February 22, 2016 at 9:05 AM

      Hi Ann,

      Thank you.
      Please check other articles too and let me know if any help needed from my side 🙂

      Reply
  29. Vishy says

    January 23, 2016 at 11:17 PM

    Awesome Mukesh!!!

    Reply
    • Mukesh Otwani says

      March 3, 2016 at 5:13 PM

      Hi Vishy,

      Thank you 🙂 Please share with your friends as well 🙂 and let me know if any help required from my end.

      Reply
  30. Karl Fokum says

    January 19, 2016 at 10:31 PM

    THanks for the help.

    Reply
    • Mukesh Otwani says

      January 20, 2016 at 12:55 AM

      Thanks Karl Fokum

      Reply
  31. manoj kumar mahato says

    January 4, 2016 at 3:55 PM

    Thanks 🙂

    Reply
    • Mukesh Otwani says

      January 6, 2016 at 1:03 AM

      Thanks Manoj

      Reply
  32. Vahe says

    December 25, 2015 at 11:49 AM

    Thanks, you help me.

    Reply
    • Mukesh Otwani says

      December 25, 2015 at 12:13 PM

      Thanks Vahe Keep visiting 🙂

      Reply
  33. salma says

    November 4, 2015 at 3:53 PM

    Very useful thank you for sharing 🙂

    Reply
    • Mukesh Otwani says

      November 5, 2015 at 1:29 AM

      Hi Salma,

      Thanks keep visiting 🙂

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?