0

I'm trying to create a class for different kind of scrolls (Scroll up, down, bottom of page, Etc), but I can't do it work.

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.chrome.ChromeDriver;

public class ScrollPage {

    public static void scrolldown(String element) throws Exception {

        System.setProperty("webdriver.chrome.driver", "C:\\JAVA\\DRIVER\\chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();       

        //Scroll 1/4 página
        JavascriptExecutor jse1 = (JavascriptExecutor) driver;
        jse1.executeScript("scroll(0,250);");
        }
    /*
        public static void scrolldown2(String element) throws Exception {       
        //Scroll hasta la mitad
        JavascriptExecutor jse2 = (JavascriptExecutor) driver;
        jse2.executeScript("scroll(0,500);");
        }

        public static void scrolldown3(String element) throws Exception {       
        //Scroll hasta el final
        JavascriptExecutor jse3 = (JavascriptExecutor) driver;
        jse3.executeScript("scroll(0,1000);");
        }
   */   
        //Otra forma de Scroll hasta el final
 /*     JavascriptExecutor jse = (JavascriptExecutor)driver;
        jse.executeScript("window.scrollTo(0,Math.max"
                + "(document.documentElement.scrollHeight,"
                + "document.body.scrollHeight,document.documentElement.clientHeight));");

    public static void scrollup(String element) throws Exception {  
        //Scroll hasta arriba
        JavascriptExecutor jse4 = (JavascriptExecutor) driver;
        jse4.executeScript("scroll(1000,0);");
        }

        public static void slowmotion (String element) throws Exception {   
        //Scroll en cámara lenta
        for (int second = 0;; second++) {
            if(second >=60){
                break;
            }
        ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,1000)", "");
        }


    }
*/
}

After that, in another class, I want to call a specific scroll:

ScrollPage.scrolldown();

Please, help me.

1

5 Answers 5

1

I see a lot of errors in your code:

  1. You are messing driver instantiation within scrolldown method. Remove all non-scroll related stuff from it.
  2. For scrolling to work, obviously, some scrollable web-page should be opened. In your scroll method no page is opened - just blank browser start page. Use driver.get(<some url>) method for that
  3. Once scrollable page is opened - call (JavascriptExecutor)driver.executeScript("window.scrollBy(0,250)", ""); to scroll window by 250 pixels
Sign up to request clarification or add additional context in comments.

1 Comment

I'm trying to call this class from another class. OOP.
0

You have used wrong java Script command to Scroll the window.

Try This :-

 JavascriptExecutor jse1 = (JavascriptExecutor) driver;
        jse1.executeScript("window.scroll(0,250);");

Comments

0

This isn't a Selenium Java issue, this has to do with object oriented programming. Are you familiar with how java classes work?

  1. Create a new class, called something like ScrollMethods, and put all your scroll methods in that class.

  2. In your test, then you just need to instantiate that class:

    ScrollMethods scrollMethods = new ScrollMethods();

Now you should be able call the Scroll Methods within your test class.

3 Comments

Hi smit9243: I don't remember very wel about the OOP, but that is what I'm trying to do so. I want to call (or invoke) the method in another class from this class.
I modified my answer, let me know if you any additional questions.
Sorry, but the issue here was because the Scroll up method (Slow motion) was not working.
0

I had some issues in my code.

The right one would be next:

package Util;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.chrome.ChromeDriver;


public abstract class ScrollPage {


	public static void doScrollDown250(ChromeDriver driver)throws Exception{
        JavascriptExecutor jse = driver;
        jse.executeScript("scroll(0, 250);");
    }
	
    public static void doScrollDown500(ChromeDriver driver)throws Exception {
    JavascriptExecutor jse2 = (JavascriptExecutor) driver;
    jse2.executeScript("scroll(0,500);");
    }
    	
    public static void doScrollUp(ChromeDriver driver)throws Exception {
        JavascriptExecutor jse2 = (JavascriptExecutor) driver;
        jse2.executeScript("scroll(0,000);");
        }
    	
    	
    	public static void doScrolldown1000(ChromeDriver driver)throws Exception {		
    	JavascriptExecutor jse3 = (JavascriptExecutor) driver;
    	jse3.executeScript("scroll(0,1000);");
    	}
      
      //Slow Motion Down
    	public static void slowmotionDown(ChromeDriver driver) throws Exception {	
    	for (int second = 0;; second++) {
    	    if(second >=60){
    	        break;
    	    }
		((JavascriptExecutor) driver).executeScript("window.scrollBy(0,13)", "");
    	}
	}
    	
    	//Slow Motion Up
    	public static void slowmotionUp(ChromeDriver driver) throws Exception {	
        	for (int second = 0;; second++) {
        	    if(second >=60){
        	        break;
        	    }
    		((JavascriptExecutor) driver).executeScript("window.scrollBy(1000,13)", "");
        	}
    	}
    	
    	//From Left to Right
    	public static void Left2Right(ChromeDriver driver) throws Exception {			
    		JavascriptExecutor js = (JavascriptExecutor)driver;
    		 js.executeScript("window.scrollBy(2000,0)", "");
    	}
    	
    	//From Right to Left
    	public static void Right2Left(ChromeDriver driver) throws Exception {		
    		JavascriptExecutor jsx = (JavascriptExecutor)driver;  
    		jsx.executeScript("window.scrollBy(-2000,0)", "");
    	}
    	
    	//Scroll Down the Element Web, in this case, Ver Detalles del Plan (Modify the Xpath)
    	public static void WebElement (ChromeDriver driver) throws Exception{
    		((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", 
    		driver.findElement(By.xpath("//*[@id='option1']/div[1]/div[5]/a")));
    	}
}

After that, in another class, we will call any of these methods with this sentence:

ScrollPage.slowmotionDown(driver);
            Thread.sleep(3000);

Please remember declarate the imports and the ChromeDriver

import org.openqa.selenium.chrome.ChromeDriver;

import Util.ScrollPage; public class banner1 extends ScrollPage {

public static void main(String[] args) throws Exception {

    System.setProperty("webdriver.chrome.driver", "C:\\JAVA\\DRIVER\\chromedriver.exe");
    ChromeDriver driver = new ChromeDriver();   

1 Comment

But I have an issue with trying to Run the Slow Motion Scroll UP: //Slow Motion Up public static void slowmotionUp(ChromeDriver driver) throws Exception { for (int second = 0;; second++) { if(second >=60){ break; } ((JavascriptExecutor) driver).executeScript("window.scrollBy(1000,13)", ""); } }
0

For Scrollpage slowmotion down, the code is:

public static void slowmotionDown(InternetExplorerDriver driver) throws Exception {
        for (int second = 0;; second++) {
            if(second >=60){
                break;
            }
            ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,13)", "");
        }
    }

For Scrollpage slowmotion up, the code is:

 public static void slowmotionUp(InternetExplorerDriver driver) throws Exception {
        for (int second = 0;; second++) {
            if(second >=60){
                break;
            }
            ((JavascriptExecutor) driver).executeScript("window.scrollBy(1000,-13)", "");
        }
    }

This is working fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.