Pages

Friday, July 3, 2015

How To scroll the page

Make sure to import org.openqa.selenium.JavascriptExecutor;


\JavascriptExecutor js = (JavascriptExecutor)driver;
// if the element is on top. js.executeScript("scroll(250, 0)"); // if the element is on bottom. js.executeScript("scroll(0, 250)");


WebElement element = driver.findElement(By.id(""));         JavascriptExecutor js =(JavascriptExecutor)driver;         js.executeScript("window.scrollTo(0,"element.getLocation().y+")");         element.click();


 You can also try the below using X or Y position
        WebElement element = driver.findElement(By.id(""));
        JavascriptExecutor js =(JavascriptExecutor)driver;
        js.executeScript("window.scrollTo(0,"element.getLocation().y+")");
        element.click();

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Great tip! Importing org.openqa.selenium.JavascriptExecutor is essential when you need to execute JavaScript directly in Selenium WebDriver. It really helps in handling elements that aren’t easily accessible via standard locators and improves test automation efficiency. online IT courses with certification

    ReplyDelete