Aug 5, 2024
26 Views
Comments Off on How does Selenium handle inner scroll?
0 0

How does Selenium handle inner scroll?

Written by

Some of WebElements is not recognized by WebDriver, WebDriver fails to find the element which is not visible in the browser’s visible area for more selenium training

In order to make the WebElement viewable by WebDriver, We need to make that element to visible in the browser’s view to do scroll down on particular div!

How can I perform my action(scroll down and click) on a particular area of my webpage. I tried a lot, doesn’t help me.

Please help me resolve my issue.

Scroll Down:

import org.openqa.selenium.JavascriptExecutor;

WebDriver driver = new FirefoxDriver();

JavascriptExecutor jse = (JavascriptExecutor)driver;

jse.executeScript(“scroll(0, 250)”); //y value ‘250’ can be altered

Scroll up:

JavascriptExecutor jse = (JavascriptExecutor)driver;

jse.executeScript(“scroll(250, 0)”); //x value ‘250’ can be altered

Scroll bottom of the Page:

JavascriptExecutor jse = (JavascriptExecutor)driver;

jse.executeScript(“window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));”);

or

Actions actions = new Actions(driver);

actions.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform();

Full scroll to bottom in slow motion:

for (int second = 0;; second++) {

    if(second >=60){

        break;

    }

    ((JavascriptExecutor) driver).executeScript(“window.scrollBy(0,400)”, “”); //y axis value is set to’400′, which can be altered

    Thread.sleep(3000);

}

or

JavascriptExecutor jse = (JavascriptExecutor)driver;

for (int second = 0;; second++) {

    if(second >=60){

        break;

    }

    jse.executeScript(“window.scrollBy(0,800)”, “”); //y value ‘800’ can be altered

    Thread.sleep(3000);

}

Scroll automatically to your WebElement:

Point hoverItem =driver.findElement(By.xpath(“Value”)).getLocation();

((JavascriptExecutor)driver).executeScript(“return window.title;”);

Thread.sleep(6000);

((JavascriptExecutor)driver).executeScript(“window.scrollBy(0,”+(hoverItem.getY())+”);”);

// Adjust your page view by making changes right over here (hoverItem.getY()-400)

or

((JavascriptExecutor)driver).executeScript(“arguments[0].scrollIntoView();”, driver.findElement(By.xpath(“Value’)]”)));

or

 WebElement element = driver.findElement(By.xpath(“Value”));

Coordinates coordinate = ((Locatable)element).getCoordinates();

coordinate.onPage();

coordinate.inViewPort();

One can go for Selenium online training to clear all doubts and queries on Selenium testing course online.

Article Categories:
AppExchange
Avatar
https://thestarbiznews.com/

Expertise: Fintech, Investment Strategies, Market Analysis Bio: Anika Kapoor is a young financial journalist with a knack for deciphering complex economic trends and translating them into engaging, digestible content. A graduate of Delhi University's prestigious Economics program, she cut her teeth as an analyst at a leading asset management firm before diving headfirst into the world of journalism. Driven by a passion for empowering individuals with financial knowledge, Anika's articles blend sharp insights with a touch of humor, making even the driest topics palatable for the everyday reader. When not poring over market data, she can be found exploring Noida's hidden gems, catching up on the latest Bollywood flicks, or indulging in her love for street photography.