0
System.out.println("****************Scrolled down starts****************");

Thread.sleep(2000);

for (int i = 0; i < 12; i++) {

JavascriptExecutor ja = (JavascriptExecutor) driver;

ja.executeScript("window.scrollBy(0,350)", "");

Thread.sleep(500);

System.out.println("Scrolled time: " + i);

}

System.out.println("****************Scrolled down end****************");



System.out.println("****************Scrolled up starts****************");

for (int i = 0; i < 12; i++) {

JavascriptExecutor jb = (JavascriptExecutor) driver;

jb.executeScript("window.scrollBy(0,-350)", "");

Thread.sleep(500);

System.out.println("Scrolled time: -" + i);

}

Here is my code i have write code in python also but it did not scroll my page

import time

from selenium import webdriver


driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://rahulshettyacademy.com/seleniumPractise/#/")
driver.maximize_window()
time.sleep(3)
print("***********************Scrolled down starts***************")
for i in range(25):
    driver.execute_script("document.body.scrollTo(0, 350);")


print("*************************scrolling down end**********************")


print("**************************************Scrolled up starts****************")

for i in range(25):
    driver.execute_script("document.body.scrollTo(0, -350);")
print("scrolling down: - " + str(i))
2
  • Hi Arslan, please do not use answers as comments. The workflow on Stack Exchange is slightly different than a typical forum. Thanks!
    – corsiKa
    Jan 18 at 16:29
  • I am also looking for this very helpful
    – richards12
    Jan 19 at 5:08
0

you are using driver.execute_script("document.body.scrollTo(0, -350);"

scrollTO will scroll to specified pixel positon , it is a one time operation as the body lenght and width remains the same.

IF you want to scroll continously use : ScrollBy

driver.execute_script("document.body.scrollBy(0, 350);"   #down
driver.execute_script("document.body.scrollBy(0, -350);"  #up

Code:

import time

from selenium import webdriver


driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://rahulshettyacademy.com/seleniumPractise/#/")
driver.maximize_window()
time.sleep(3)
print("***********************Scrolled down starts***************")
for i in range(25):
    time.sleep(2)
    driver.execute_script("document.body.scrollTo(0, 350);")


print("*************************scrolling down end**********************")


print("**************************************Scrolled up starts****************")

for i in range(25):
    time.sleep(2)
    driver.execute_script("document.body.scrollTo(0, -350);")
    print("scrolling down: - " + str(i))
12
  • i have changed it but did not work Jan 18 at 16:23
  • in java it work fine but did not understand why it did not work in python kindly check and tell me how we write code that work fine Jan 18 at 16:26
  • @ArslanHassan what you mean by it didn't work, i just ran the code and it works.
    – PDHide
    Jan 18 at 16:34
  • in console it show no error but did not scroll page kindly once run it on our compute this code and then tell me what is the problem Jan 18 at 16:35
  • i ran your code only just add time.sleep(5) in the loop , it happens so fast that you wn't notice the scroll
    – PDHide
    Jan 18 at 16:36
0

In the Java code you are using .scrollBy() but in your python code you are using .scrollTo(). My guess is that it is not scrolling because of this mixup. Try changing the python code to use .scrollBy() method

Not the answer you're looking for? Browse other questions tagged or ask your own question.