blagojkk
blagojkk

Reputation: 21

human like mouse scroll in java and selenium

First I would like to say that I started programming 3 days ago. So I'm pretty much a beginner. I do not plan on engaging in programming other than what I present here, I just need a simple program for now and I wanted to do it by myself, purely as a hobby. I'm doing it with Selenium and java. So, I need a code that will scroll down/up the web application in the web browser as much as human real as possible. I'm currently using this code:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript ("window.scrollBy (0.1500)", "");

and it's okay, however what this code do looks like pressing page down button instead of scrolling.

And after that, I thought to do the following:

js.executeScript ("window.scrollBy (0.50)", "");
js.executeScript ("window.scrollBy (0.30)", "");
js.executeScript ("window.scrollBy (0.33)", "");
js.executeScript ("window.scrollBy (0.42)", "");
js.executeScript ("window.scrollBy (0.45)", "");
js.executeScript ("window.scrollBy (0.24)", "");
js.executeScript ("window.scrollBy (0.19)", "");
js.executeScript ("window.scrollBy (0.50)", "");

so now this code scrolls the page down like scrolling with mouse using the scroll wheel, but as you can see, it does not look like an "elegant" solution, and I'm sure that there is something better, but with my 3 days experience this is the best I can do.

So, is there anyone willing to help me more efficient code instead of using millions of lines to do the same repeat job... I don't know, something like:

js.executeScript ("window.scrollBy (0,50,45,66,78,43,56,87)", "");

where in one line and several variables the program can do what need to be done and take the time intervals and/or the pixels on which to scroll from "0,50,45,66,78,43,56,87 "

Upvotes: 2

Views: 3491

Answers (2)

Ali Azam
Ali Azam

Reputation: 2115

Try to implement arguments[0].scrollIntoView(true); in the executeScript() method.

If you want to scroll down to last, select FOOTER as the target element.

WebElement elem =  driver.findElement(By.id("ID OF FOOTER ELEMENT"));

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView(true);", elem);

You can use By.className("CLASSNAME OF FOOTER ELEMENT") or By.xpath("XPATH SELECTOR OF FOOTER") or By.cssSelector("CSS SELECTOR OF FOOTER") if id is not available.

Note: You can scroll to any position locating the target element selector.

Upvotes: 1

mcd
mcd

Reputation: 726

First let us use an array to store your coordinates or values.

JavaScript Arrays

An array can hold many values under a single name, and you can access the values by referring to an index number following this simple syntax:

var array_name = [item1, item2, ...];

For your example that could then look like this:

var values = [0.50, 0.33, 0.33, 0.42, 0.45, 0.24, 0.19, 0.50];

Now you could create your own function for the scrolling that uses all of the values stored in the array created above. combined with a loop this would get repeated.

function scrollFunction() {

    // Creating our needed variables
    var values, vLength, I;

    // Filling the array
    values = [0.50, 0.33, 0.33, 0.42, 0.45, 0.24, 0.19, 0.50];
    vLength = values;

    // By using this for-loop this process will repeat with with all of the variables from the array. However the next value is used, not the same one every time!
    for (i = 0; i < vLength; i++) {
        window.scrollBy (values[i], ""); // Update: deleted one ")"
    }
}

To create a smooth scrolling effect I would recommend to use the scroll function differently:

window.scrollBy({ top: values[i], left: 0, behavior: 'smooth' });

Upvotes: 0

Related Questions