Interview Questions

Selenium WebDriver - How do you simulate scroll down action ?

Selenium WebDriver Interview question and Answers


(Continued from previous question...)

Selenium WebDriver - How do you simulate scroll down action ?

Use java script to scroll down-
JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("window.scrollBy(0,4500)", "");
//scroll down, value 4500 you can change as per your req jsx.executeScript("window.scrollBy(450,0)", "");
//scroll up ex- public class ScrollDown { public static void main(String[] args) throws InterruptedException { WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.flipkart.com/womens-clothing/pr?sid=2oq,c1r&otracker=hp_nmenu_sub_women_1_View%20all");
driver.manage().window().maximize(); JavascriptExecutor jsx = (JavascriptExecutor)driver; jsx.executeScript("window.scrollBy(0,4500)", "");
//scroll down Thread.sleep(3000); jsx.executeScript("window.scrollBy(450,0)", ""); //scroll up } }

JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("window.scrollBy(0,4500)", "");
//scroll down, value 4500 you can change as per your req
jsx.executeScript("window.scrollBy(450,0)", ""); //scroll up

ex-
public class ScrollDown {

public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.flipkart.com/womens-clothing/pr?sid=2oq,c1r&otracker=hp_nmenu_sub_women_1_View%20all");
driver.manage().window().maximize();
JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("window.scrollBy(0,4500)", ""); //scroll down
Thread.sleep(3000);
jsx.executeScript("window.scrollBy(450,0)", ""); //scroll up

}

}

Use java script to scroll down- JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("window.scrollBy(0,4500)", "");
//scroll down, value 4500 you can change as per your req jsx.executeScript("window.scrollBy(450,0)", "");
//scroll up ex- public class ScrollDown { public static void main(String[] args) throws InterruptedException { WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.flipkart.com/womens-clothing/pr?sid=2oq,c1r&otracker=hp_nmenu_sub_women_1_View%20all");
driver.manage().window().maximize(); JavascriptExecutor jsx = (JavascriptExecutor)driver; jsx.executeScript("window.scrollBy(0,4500)", "");
//scroll down Thread.sleep(3000); jsx.executeScript("window.scrollBy(450,0)", ""); //scroll up } }

JavascriptExecutor jsx = (JavascriptExecutor)driver;
v jsx.executeScript("window.scrollBy(0,4500)", ""); //scroll down, value 4500 you can change as per your req

jsx.executeScript("window.scrollBy(450,0)", ""); //scroll up

ex-

public class ScrollDown {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.flipkart.com/womens-clothing/pr?sid=2oq,c1r&otracker=hp_nmenu_sub_women_1_View%20all");
driver.manage().window().maximize();
JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("window.scrollBy(0,4500)", ""); //scroll down
Thread.sleep(3000);
jsx.executeScript("window.scrollBy(450,0)", ""); //scroll up
}
}

(Continued on next question...)

Other Interview Questions