Using the Selenium webdriver to reach the bottom of the page by scrolling vertically

Is there a way to determine if I have scrolled to the bottom of the page vertically?

I have been using Webdriver and pressing the page down key repeatedly within a for loop, like so:

for(int di=0; di<40; di++) { 
   driver.findElement(By.tagName("body")).sendKeys(Keys.PAGE_DOWN);
   Thread.sleep(1500);
}

However, my for loop always runs 40 times regardless of the actual size of the page. I am looking for a solution to accurately detect when I have reached the end of the page so that I can break out of the loop at that point.

Answer №1

To start, you should select an element located at the bottom of the webpage. Let's assume there is a footer tag with the unique identifier company-email.

boolean flag = true;
while(flag){
if(driver.findElements(By.id("company-email")).size()>0){

    flag = false;
}
else{

   driver.findElement(By.tagName("body")).sendKeys(Keys.PAGE_DOWN);
   Thread.sleep(1500);
   }
}

Answer №2

If you need to scroll to the end of a page, you can use the following method:

public static void scrollToEndOfPage(WebDriver driver) {
        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
    }

Once this code has been executed, you can continue with other operations.

To scroll to a specific WebElement on the page, you can use the following method:

 public static void scrollToElement(WebDriver driver, WebElement element) {
            ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", element);
        }

I hope this information is helpful to you.

Answer №3

Thank you for the encouragement! I finally came up with an idea and successfully implemented it to meet my expectations.

The first step is to calculate the scroll height.

 JavascriptExecutor executor = (JavascriptExecutor) driver;
            long value = (Long) executor.executeScript("return document.body.scrollHeight");

Next, create a loop to scroll through the page.

for (int f = 500; f <= value; f += 500) {
            JavascriptExecutor jsc = (JavascriptExecutor) driver;
            jsc.executeScript("window.scrollTo(0," + f + ")");
            Thread.sleep(1500);
        }

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

Partial implementation of jQuery datepicker feature facing technical issues

Greetings, I have a functional datepicker implemented in my code as follows: <link rel="stylesheet" href="uidatepicker/themes/blitzer/jquery.ui.all.css"> <script src="uidatepicker/jquery-1.5.1.js"></script> <script src="uidate ...

Issue with CSV download box not showing up on Rails version 2.3.11

I've encountered an issue while trying to export a csv file with some data. I am using ajax call to select rows from the view table (jqGrid) and export them in a csv format. However, after a successful ajax call, the filtered data is displaying as an ...

Can the selected week be highlighted along with the corresponding week number in a row?

Can we display the number of the week in a row along with the selected week, either in the toolbar or somewhere else? I attempted to utilize ToolbarComponent, but it overrides the entire header. However, I would like to keep it as is and just add informat ...

Discover an Effective Approach for Transmitting Form-Data as a JSON Object

Hey there! I'm encountering a bit of an issue with sending some data as a JSON object. The problem arises when trying to send images using FormData. It seems like I need to convert my form data into a single JSON object. Can anyone assist me with this ...

Tips for executing a callback function when triggering a "click" event in jQuery?

Having trouble triggering a custom event in the callback of a trigger call. Attempted solutions: var $input = $( ".ui-popup-container" ).find( "input" ).eq(2); function runtests () { console.log("clicked the input"); }; $input.trigger('click&ap ...

Transform a flat 2D shape into a dynamic 3D projection using d3.js, then customize the height based on the specific value from ANG

Currently, I am utilizing d3.js version 6 to generate a 3D representation of the 2D chart shown below. Within this circle are numerous squares, each colored based on its assigned value. The intensity of the color increases with higher values. https://i.ss ...

Using JavaScript to listen for events on all dynamically created li elements

Recently, I've created a simple script that dynamically adds "li" elements to a "ul" and assigns them a specific class. However, I now want to modify the class of an "li" item when a click event occurs. Here's the HTML structure: <form class ...

Is your Scrollmagic failing to function once your website is uploaded to the server?

My website incorporates Scrollmagic to dynamically load sections (changing opacity and adding movement) as users scroll through it. Everything functions perfectly fine when I preview the HTML file on my computer, but once I uploaded it to my hosting serv ...

Experience Play! 2.1-RC2 JavaForms validate() function with dynamic references

As I delve into the Play! 2.1 example to establish a simple login system inspired by the ZenTasks demo, I encounter some hurdles in the JavaForms segment. Specifically, I am aiming to validate the login request using an instance of an auth service provided ...

iOS devices experiencing issues with fixed positioning

Currently, I am in the process of implementing a few instances of , but I am encountering some issues with the CSS. When viewing the website on an iPad or iPhone, the calendar is positioned correctly as the container covers the window like a fixed position ...

What are the best ways to conceptualize the benefits of WebRTC?

I encountered a peculiar issue with the abstraction of the WebRTC offer generation process. It appears that the incoming ice candidates fail to reach the null candidate. While I have been able to generate offers successfully using similar code in the past, ...

Using Selenium for Web Scraping and attempting to rectify a parallelization error with ThreadPool approach

After spending time on Web Scraping for reviews websites and finding it exhausting to run the code, I realized that extracting 20k reviews takes at least a day. In order to address this issue, I started exploring new possibilities, with parallelizing task ...

Dynamic starting point iteration in javascript

I'm currently working on a logic that involves looping and logging custom starting point indexes based on specific conditions. For instance, if the current index is not 0, the count will increment. Here is a sample array data: const data = [ { ...

Selenium - Struggling to locate the element

Having trouble with clicking a drop-down menu specifically on the second line from the bottom. Selenium keeps throwing an error saying it cannot locate the element I want to click. I've tried using Xpath and ID instead of Class name, but that didn&apo ...

Triggering an event from a component to its parent module resulting in an exception situation

Here is my app.component.ts code: import { Component, Input, OnInit, OnChanges, SimpleChanges} from '@angular/core'; import {Counter } from './counter' @Component({ selector: 'my-app', template: ` <custom-counter [ ...

How to iterate through a "for" loop in JavaScript using Python-Selenium?

In my current project, I am utilizing Javascript to gather data from an HTML page using Selenium. However, I am facing a challenge where I am unable to execute the multi-line for loop in the Javascript portion on my computer through Selenium with Python (S ...

Is it possible to store a JWT token in local storage when working with Next.js?

We are considering using Next.js for our application, with a focus on client-side rendering for data fetching. The API we will be interacting with is external and requires authentication to access specific user dashboard content. While the homepage will ...

Issue with selenium in the Anaconda Navigator command prompt on Windows 10

Upon attempting to begin with the jupyter lab command on the anaconda navigator terminal, I encountered an error message stating "ModuleNotFoundError: No module named 'selenium'." Even though selenium appears to be installed correctly through th ...

Exploring the functionality of window.matchmedia in React while incorporating Typescript

Recently, I have been working on implementing a dark mode toggle switch in React Typescript. In the past, I successfully built one using plain JavaScript along with useState and window.matchmedia('(prefers-color-scheme dark)').matches. However, w ...

Utilizing ASCII art in React: A guide to incorporating textual designs into your

I'm having trouble displaying ASCII images correctly on my React app. I've tried various methods, but nothing seems to maintain the form when rendered in the browser. <Grid container id="terminal_banner"> <Grid item ...