Page Scroll Up or Down in Selenium Webdriver (Selenium 2) Using Java

Page scroll up or down in Selenium WebDriver (Selenium 2) using java

Scenario/Test steps:
1. Open a browser and navigate to TestURL

2. Scroll down some pixel and scroll up

For Scroll down:

WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,250)");

OR, you can do as follows:

jse.executeScript("scroll(0, 250);");

For Scroll up:

jse.executeScript("window.scrollBy(0,-250)");
OR,
jse.executeScript("scroll(0, -250);");

Scroll to the bottom of the page:

Scenario/Test steps:
1. Open a browser and navigate to TestURL

2. Scroll to the bottom of the page

Way 1: By using JavaScriptExecutor

jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");

Way 2: By pressing ctrl+end

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL, Keys.END);

Way 3: By using Java Robot class

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_END);
robot.keyRelease(KeyEvent.VK_END);
robot.keyRelease(KeyEvent.VK_CONTROL);

How to Scroll down with Selenium?

To invoke click() on the link with the text Privacy Policy you simply need to induce WebDriverWait for the desired element to be clickable, then again induce WebDriverWait for the desired element to be visible and then scroll in to view and you can use the following solution:

  • Code Block:

    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;

    public class insly_Privacy_Policy {

    public static void main(String[] args) {

    System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
    WebDriver driver=new FirefoxDriver();
    driver.get("https://signup.insly.com/signup");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("privacy policy"))).click();
    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated((By.xpath("//div[@id='document-content']//following::div[contains(.,'Revision')]"))));
    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
    }
    }
  • Browser Snapshot:

revision

How to scroll down using Selenium WebDriver with Java

Try using simple java script below and you can scroll the page.

JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("window.scrollBy(0,450)", "");

Selenium: Scroll to end of page in dynamically loading webpage

I will provide you code in Python for this. I think it's easy to translate to Java:

def scroll_down(self):
"""A method for scrolling the page."""

# Get scroll height.
last_height = self.driver.execute_script("return document.body.scrollHeight")

while True:

# Scroll down to the bottom.
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

# Wait to load the page.
time.sleep(2)

# Calculate new scroll height and compare with last scroll height.
new_height = self.driver.execute_script("return document.body.scrollHeight")

if new_height == last_height:

break

last_height = new_height

Hope it helps you!

Vertical Scroll down and scroll up in Selenium WebDriver with java

You can scroll down vertically by using the following code:

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

Similarly, it is also possible to scroll up by changing y coordinate as negative:

((JavascriptExecutor) driver).executeScript("scroll(0, -250);");

You can also use the following code:
For scroll down:

((JavascriptExecutor) driver).executeScript("window.scrollBy(0,250)", "");

For scroll up:

((JavascriptExecutor) driver).executeScript("window.scrollBy(0, -250)", "");

scroll up the page to the top in selenium

To scroll to the top of the page, just scroll to the 0, 0:

window.scrollTo(0, 0);

Or, as an alternative option, you can scroll into view of the header element (or some other element on top):

WebElement element = driver.findElement(By.tagName("header"));

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].scrollIntoView();", element);


Related Topics



Leave a reply



Submit