nosequeweaponer
nosequeweaponer

Reputation: 668

Selenium Webdriver (Java) Creation a class for Scrolling

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.

Upvotes: 0

Views: 818

Answers (5)

nosequeweaponer
nosequeweaponer

Reputation: 668

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.

Upvotes: 0

nosequeweaponer
nosequeweaponer

Reputation: 668

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();   

Upvotes: 0

smit9234
smit9234

Reputation: 361

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.

Upvotes: 0

Ankur Singh
Ankur Singh

Reputation: 1289

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

Try This :-

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

Upvotes: 0

Ivan Pronin
Ivan Pronin

Reputation: 1896

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

Upvotes: 1

Related Questions