SCROLLING A PAGE
We can use java scroll down in the page in selenium the java script to scroll down the page in selenium in window scroll by (0.250).To creat the java inside the java program, we can take help of the execute script().Which is available in java script execution inferface
There types of scrolling pages
1.To scroll down the web page by pixel
Syntax
JavaScriptExecutor Jse= (JavaScriptExecutor)driver;
Jse.executeScript(“window.scrollBy(0, 1000)”, “ ”);
2.To scroll down the web page by the visibility of the element
Syntax
Driver.findElement(By.xpath(“("//button[text()='Generate Confirm Box']"));
webElement Scroll=JavaScriptExecutor Jse= (JavaScriptExecutor)driver;
Jse.executeScript(“arguments[0].scrollIntoView();”, Scroll);
3.To scroll down the web page at the bottom of the page
Syntax
JavaScriptExecutor Jse= (JavaScriptExecutor)driver;
Jse.executeScript(“window.scrollTo(0,document.body.scrollHeight)”);
PROGRAM
package mypackage;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Scrolling {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","C:\\Browser\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.google.com/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
scroll down the web page by pixel
driver.navigate().to("https://wynk.in/music");
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript("window.scrollBy(0,1000)");
OR
driver.navigate().to("https://wynk.in/music");
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript("scroll(0,1000);");
js.executeScript("scroll(0,-1000);");
scroll down the web page by the visibility of the element
driver.navigate().to("https://www.amazon.in/");
WebElement scroll= driver.findElement(By.xpath("//div[@class='navFooterLine navFooterLogoLine']"));
js.executeScript("arguments[0].scrollIntoView();", scroll);
scroll down the web page at the bottom of the page
driver.navigate().to("https://www.selenium.dev/");
js.executeScript("window.scrollTo(0,document.body.scrollHeight)");
USING ROBOT CLASS
driver.navigate().to("https://wynk.in/music");
Robot scroll=new Robot();
scroll.keyPress(KeyEvent.VK_PAGE_DOWN);
scroll.keyRelease(KeyEvent.VK_PAGE_UP);
USING ACTION CLASS
driver.navigate().to("https://wynk.in/music");
Actions scroll=new Actions(driver);
scroll.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform();
scroll.keyDown(Keys.CONTROL).sendKeys(Keys.HOME).perform();
}
}