Tips for scrolling a div that has too much content within a webpage

Is there a method to manipulate the scrollbar within a div on a webpage? Specifically, I am attempting to automate scrolling up and down on an Instagram post like . However, since the scrollbar may be hidden using CSS properties, detecting it can be challenging as the scrollbar itself is not considered an element.

Below is the CSS style (from Firebug) of the scrollbar:

.-cx-PRIVATE-PostInfo__comments {
    margin-left: -24px;
    margin-right: -24px;
    margin-top: -5px;
    padding-left: 24px;
    padding-right: 24px;
    padding-top: 5px;
}
.-cx-PRIVATE-PostInfo__commentsSidebarVariant {
    overflow: auto;
    padding-bottom: 20px;
} 
.-cx-PRIVATE-PostInfo__comments {
    flex-grow: 1;
}
ol, ul {
    list-style: outside none none;
}
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, 
blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, 
img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, 
center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, 
tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, 
figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, 
time, mark, audio, video {
    border: 0 none;
    font: inherit;
    margin: 0;
    padding: 0;
    vertical-align: baseline;
}

I have attempted various methods such as

WebElement commentscroll = dr.findElement(By.className("commentsSidebarVariant"));

jse.executeScript("return arguments[0].scrollTop;", commentscroll);

jse.executeScript("$(\"#commentsSidebarVariant\").animate({ scrollTop: \"100px\" })");  

jse.executeScript("arguments[0].scrollTop = arguments[1];", commentscroll);

WebElement commentscroll = dr.findElement(By.cssSelector(".-cx-PRIVATE-PostInfo__commentsSidebarVariant"));               

jse.executeScript("arguments[0].scrollTop;", commentscroll);

However, none of these attempts were successful in moving the scrollbar.

Answer №1

My approach to scrolling an overflowing element using pure JS involves checking if I have reached the end of the scroll.

The code snippets below serve as a reference for implementing the solution:

    // Determining if scrolling down was successful
    private boolean scrollDownOverflowElement(WebElement element, int lazyLoadingGracePeriodMillis) {
        long scrollTopBefore = (long) javascriptExecutor.executeScript("return arguments[0].scrollTop", element);
        javascriptExecutor.executeScript("arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].clientHeight", element);
        if (isAtEndOfScroll(element)) {
            sleep(lazyLoadingGracePeriodMillis); // Waiting for lazy loading
        }
        long scrollTopAfter = (long) javascriptExecutor.executeScript("return arguments[0].scrollTop", element);
        return scrollTopAfter != scrollTopBefore;
    }

    // Checking if element is at end of scroll
    private boolean isAtEndOfScroll(WebElement element) {
        final boolean isAtEnd = (boolean) javascriptExecutor.executeScript("return arguments[0].scrollHeight - arguments[0].scrollTop === arguments[0].clientHeight", element);
        return isAtEnd;
    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to make a HTML div background fill the entire page

Is there a way to extend the background in a div so that it adjusts to fit the screen resolution? I also want to include a navigation bar at the top right corner of this div for the intro page. There will be additional content in other divs positioned be ...

What is the best way to arrange a number of inline-block elements with equal size in a vertical alignment?

My goal is to display a set number of items in a container, all with the same fixed size. The challenge arises when the content of each item varies in length, causing misalignment vertically: .child { display: inline-block; background: #bbbbbb; marg ...

Prevent the wrapping of elements in an expanded hover state

Check out the Latest Demo: https://fiddle.jshell.net/Benihana77/cjtg5ojf/ The Scenario: I have designed a versatile promo where the main text and the button/link text can vary in length. The button/link is placed inline, with an extended arrow that expan ...

What is the best way to make the text align with the top of its designated space?

I've been experimenting with different CSS properties like vertical-align, line-height, and more. This example should give you an idea of my goal. I added a small red arrow to indicate where I intend for the text to be shifted upwards. ...

The border of the cell in the top row only partially overlaps with the margin area

Have you noticed how the left border of the first cell doesn't line up with the margin area of the table? This peculiar phenomenon seems to occur only in the first row: https://i.stack.imgur.com/qMWCh.jpg In all other rows, the border aligns proper ...

Tips for utilizing a For loop to capture user-inputted information?

Is there a way to modify my code so that when I input a number/integer, the program will read it instantly and move on without displaying the inputted integer at the bottom? This is similar to the expected output illustrated in the picture. Below is my cu ...

<v-time-picker> - issue with time selection

<v-time-picker> - Are you certain that the component was properly registered? If dealing with recursive components, remember to include the "name" option <div class="form-inline"> <label for="">Time</label> <v-time-pick ...

Observing a web page created by a Java Servlet

After receiving the provided code, I attempted to compile it using a customized version of Dr. Java. However, upon attempting to run the code, I encountered an error message stating: "Static Error: This class does not have a static void main method accepti ...

Understanding the concept of for loops in JavaScript and incorporating them in CSS styling

Hello there! I initially used this code to draw 12 lines, but now I am looking to incorporate a for loop and implement it in CSS. Can someone please guide me on how to proceed? <!DOCTYPE html> <html> <head> <style> #straigh ...

The appearance of the logo varies across various web pages on the site

Isn't it strange that my logo appears pixelated only on the home page, even though I used the same HTML and CSS code for all pages? The Html: <a href="Home.html"><img id="logo" src="../CONTENT/Images/Logo/1Sr2l8а.png" alt="logo"/></ ...

How to display a video with full height and width using CSS or JavaScript

I am trying to incorporate an html5 video (using the video tag) with 100% width and 100% height to play in the background. I have seen an example using an image, but I want to achieve the same effect with a video. Here is the code I have so far: #video { ...

Guide to creating a dynamic column layout with minimum height divs

I am facing an issue with the variable height of my div, causing it to position incorrectly. To demonstrate the problem, I have included a code snippet below. Here is the solution I am seeking: https://i.sstatic.net/XMAMr.png Note: Since the div has a ...

Is the container in semantic ui being breached by a segment overflow?

I've recently started learning Semantic UI. One issue I'm facing is that the width of the "segment" overflows the "container." Check this JSFiddle for more clarity. Are there any alternative solutions? In addition to the grid system, I'm ...

Creating interactive tables in HTML and Javascript where users can expand and collapse rows by clicking on a parent row

After days of trying to solve a specific issue, I have realized that I cannot do it without some help. The task at hand is to enable the functionality of clicking on a table row to reveal more details, but in my case, these additional details are presented ...

The issue of flickering while scrolling occurs when transitioning to a new page in Angular

I have encountered an unusual issue in my Angular 13 + Bootstrap 5 application. When accessing a scrollable page on small screen devices, the scrolling function does not work properly and causes the page to flicker. I observed that this problem does not o ...

Retrieving information from a web address is displaying unusual characters

Having trouble retrieving data from a JSON file using Java. When accessing the URL in a browser, the data displays correctly. However, when attempting to retrieve the data with Java, I am getting unreadable characters that cannot be parsed. This code works ...

Unable to showcase information in the center of an HTML document

Hello, I'm facing an issue with my HTML page that has a left vertical nav-bar. Despite my efforts, I can't seem to display content (text) in the center of the page as shown in the screenshot with the red oval. I've attempted inserting text ...

Is there a way to choose an item from a dropdown menu in Selenium using Python if the option's element is not clickable?

I am encountering an issue while trying to select an item from the dropdown menu. The options that I need to choose from are not selectable and display an alert message stating, "Alert: This element is not interactable through selenium(automation) as it is ...

React Hamburger Menu not working as intended

I am facing an issue with my responsive hamburger menu. It is not functioning correctly when clicked on. The menu should display the navigation links and switch icons upon clicking, but it does neither. When I remove the line "{open && NavLink ...

In IE11 on Windows 10, the method Driver.getWindowHandles() consistently returns a single window handle

It seems that this question has already been asked before with similar issues: Driver.getWindowHandles() is always returning 1 in IE11 on Windows 10, although there are two windows open Selenium - getWindowHandles() is returning value 1 irrespective of n ...