Guide on how to navigate to the bottom of a div element using Selenium Webdriver

My current project involves a unique challenge where there is a specific div element on the webpage that acts as a popup dialog when a link is clicked (similar to Facebook reaction dialog boxes).

To automate tests for this scenario, I am using Selenium WebDriver with Java. The task at hand requires me to scroll to the bottom of the dialog box where there is a "Show More" link. Clicking this link loads an additional 10 items in the list, and this process continues until all items have been displayed.

The challenge lies in continuously scrolling down within that specific div element to locate the "Show More" link. If the driver cannot find the link, the scrolling should stop.

It is important to note that a simple JavaScript `window.scrollTo()` function cannot be used to scroll to the bottom of the whole page; rather, the goal is to scroll only within that particular division element.

If anyone has any suggestions or solutions on how to achieve this step-by-step scrolling within the div element, please share your insights.

Thank you in advance for your assistance!

Answer №1

A different method for navigating to the bottom within a scrollable container:

function scrollToBottomInsideElement(element) {
    const jsExecutor = (JavascriptExecutor) driver;
    jsExecutor.executeScript("arguments[0].scrollTo(0, arguments[0].scrollHeight)", element);
}

Answer №2

WebElement elementToScroll= driver.findElement(By.id("id_of_element"));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", elementToScroll);

or

WebElement elementToClick= driver.findElement(By.id("id_of_element"));
Actions actions = new Actions(driver);
actions.moveToElement(elementToClick).click();
Action actionPerform=actions.build();
actionPerform.perform();

Answer №3

I encountered a similar issue and managed to resolve it by referencing a question on SQA SO. You can find the helpful post here.

Take a look at the final answer provided by Sagar007, who implemented a separate function scroll_Page() to address this issue.

The code snippet below is directly from his solution:

Some HTML pages have custom internal scroll bars that require a different approach. Javascript may not be effective in these cases.

Solution :

WebElement scrollArea = 
driver.findElement(By.cssSelector("div.slimScrollBar"));

In my implementation, I included an additional step of clicking on the div element using the click() method before proceeding further.

To create the scroll_Page method as outlined below and invoke it as required.

 scroll_Page(scrollArea ,100);

Where scrollArea represents the element to be scrolled and 100 indicates the number of scroll points.

  public static boolean scroll_Page(WebElement webelement, int scrollPoints)
{
try
{               
    Actions dragger = new Actions(driver);
    // drag downwards
    int numberOfPixelsToDragTheScrollbarDown = 10;
    for (int i = 10; i < scrollPoints; i = i + numberOfPixelsToDragTheScrollbarDown)
    {
        dragger.moveToElement(webelement).clickAndHold().moveByOffset(0, numberOfPixelsToDragTheScrollbarDown).release(webelement).build().perform();
    }
    Thread.sleep(500);
    return true;
}
catch (Exception e)
{
    e.printStackTrace();
    return false;
}

Answer №4

In order to achieve scrolling functionality for a dynamically loaded div on a webpage, I utilized a Java function that was called within a while(someCondition()) loop. The approach involved incrementing the scroll value by 1000 units each time the function was triggered.

private int scroll;
private void scrollDownPopup() {
    scroll += 1000;
    JavascriptExecutor jse = (JavascriptExecutor) browser;
    // To determine the query selector for the specific div element, use an inspector tool
    jse.executeScript("document.querySelector('body > div.Blah.Blah1 > div > div.ClassOfYourDiv').scrollTo(0, " + scroll + ");");
}

Answer №5

One useful method to utilize is scrollIntoView

Comparing scrollIntoView and moveToElement Understanding the difference between scrollIntoView and moveToElement

Ensure that you pass the link element for "more items" as an argument.

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

Another option is to use moveToElement

Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();

Answer №6

To reach the bottom of a specific element by scrolling, one can employ the following method:

public void goToBottomOfElement(WebElement target) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("arguments[0].scrollIntoView({block: 'end'})", target);
    }

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

Experiencing prolonged delays with PHP/AJAX login process

Recently, I took over the maintenance of a website that requires users to log in. However, I've noticed that the login process is quite slow, especially when there are around 4000 registered users. The current login system utilizes a combination of A ...

Tips on implementing a jQuery .load() function using an anchor tag within dynamic content

I am working on a search page where user input is taken from a form and then sent to a PHP file that makes a cURL call to an external server. The PHP file receives an array from the server, which it uses to display HTML in a "results" div on the original s ...

Is there a way to create a Vue component that can process dynamic formulas similar to those used in

I am looking to create a component that has the ability to accept different formulas for computing the last column. The component should use these formulas in Vuex getters to store the total state values passed to it. Here are the specifications for the c ...

"Exciting Changes in Color According to the Active State of vue-route-link

I am trying to find a way to customize the CSS based on whether a link is exact or active. Essentially, when a user clicks on a menu item, I want the underline to change depending on whether the link is an active router-link. Although I was able to accompl ...

Difficulty transferring information between two components by using services

I am trying to pass the values of an array from the Search component to the History component in order to display the search history. My current code structure looks like this: search-page.component.ts export class SearchPageComponent implements OnInit ...

Fetch response headers not being detected by Web Worker

Currently in my chrome extension, I'm utilizing web workers to collect response header cookies from a specific website. Interestingly, when I execute the request on the main thread, the response contains all the expected cookies. However, when the exa ...

What is the proper way to delete a callback from a promise object created by $q.defer() in AngularJS?

When working with AngularJS, the $q.defer() promise object has the ability to receive multiple notify callbacks without overwriting previous ones. var def = $q.defer(); def.promise.then(null, null, callback1); def.promise.then(null, null, callback2); If ...

The functionality of List.js is currently not optimized for use with tables

I'm currently experimenting with list.js in order to create a real-time search feature for a table. I have successfully tested it on lists (similar to the example provided at ). However, I am facing difficulty replicating this functionality for tables ...

Creating flexible Vue routes using a single router component and a navigational menu

I am working on a Vue project and I want to implement nested routes in my router.js. My goal is to have just one menu for all the nested routes and only one <router-view></router-view> in a master component. This is what I envision (in pseudoc ...

What is the method to retrieve the local filepath from a file input using Javascript in Internet Explorer 9?

I'm experiencing some issues with the image-preview function on IE9, similar to the example photo shown. This method is not functioning properly and throwing an error in IE9, while it works perfectly fine in IE6-8. I am looking for a way to retrieve ...

Switch between two tabs with the convenient toggle button

I have implemented 2 tabs using Bootstrap as shown below: <ul class="nav nav-tabs" role="tablist"> <li role="presentation" class="active"><a href="#home" role="tab" data-toggle="tab">CLient</a></li> <li role="presentatio ...

How to dynamically change the position of an input field within a form using JavaScript and jQuery

I have a form displayed on my website that looks like this: input a input b input c I am wondering if it is achievable to rearrange the order of inputs using jQuery, like so: input a input c input b However, it is important that the data is still ...

Tips for including an external babel JS (ES6) file in an HTML document:

To include the babel js file in an HTML file, take a look at the code snippet below: <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudfl ...

How to extract the title of the third window with Selenium Webdriver in Java

My Scenario is: I start with the base URL: www.****.com After opening the URL, I click on a link labeled "Test". This opens another window, let's call it Window B. In Window B, I then click on button "button1", which in turn opens another window nam ...

Obtaining values from event listeners in Vue.js across different methods

Looking for guidance on the code snippet provided below. Is there a way to assign the value of "e.detail.name" to "nodeName," and then access it in a different method within the component for an API request? data() { return { nodeName: '' ...

How to Use PHP to Submit Form Information

I am a beginner in PHP and I need help with sending form details to an email address. I have tried looking for solutions online but I keep running into the same issue - when I submit the form, it downloads the PHP file instead of sending an email. Below i ...

React useEffect alert: Exceeding maximum update depth limit. Any solutions to bypass this issue?

In the code snippet below, I am utilizing the useEffect hook to monitor changes to a percentage variable and then initiating a timer to increment that variable every second. This process starts as soon as the page loads. The percentage variable is crucial ...

How can you determine whether the CSS of a <div> is defined in a class or an id using JavaScript/jQuery?

Hey there, I'm currently working on dynamically changing the CSS of a website. Let's say we have a div like this: <div id="fooid" class="fooclass" ></div> The div has a class "fooclass" and an ID "fooid", and we're getting its ...

Tips for aligning text in MUI Breadcrumbs

I am currently utilizing MUI Breadcrumb within my code and I am seeking a solution to center the content within the Breadcrumb. Below is the code snippet that I have attempted: https://i.stack.imgur.com/7zb1H.png <BreadcrumbStyle style={{marginTop:30}} ...

Is Formik Compatible with TextareaAutosize?

I've implemented react-textarea-autosize and formik in my project, but I'm having trouble connecting the change events of formik to TextareaAutosize. Can anyone guide me on how to do this properly? <Formik initialValues={{ ...