MainMenu

Home Java Overview Maven Tutorials

Tuesday, 23 January 2018

How to scroll browser in Selenium


Hello friends,

In this article we will discuss that how to perform scroll operation in selenium.

For Video :- Click Here



Watch Video for more Understanding





While automating an application we generally found that we need to perform here scroll Up or scroll down or Scroll up to an element, So in this demo we will learn that how to perform these all operations.

In selenium, we have JavascriptExecutor interface, to perform the scroll operation, which provide us a method "executeScript".

1). How to Scroll up :
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,-500)", "");

2). How to Scroll Down :
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0, 500)", "");

3). How to Scroll Up to an element :
WebElement webelement = driver.findElement(By.xpath("xpath of your elements"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollIntoView(true);", webelement);

4). How to Scroll up to bottom of webpage:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");

Below is the example that how to perform the above three operation in some realtime application :

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Scroll_demo
{
WebDriver driver;
@BeforeTest
public void navigate() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\Complete selenium\\ChromeDriver\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.navigate().to("http://www.way2testing.com");
Thread.sleep(3000);
}
@Test
public void scrollTest()
{
WebElement link = driver.findElement(By.xpath(".//*[@id='post-body-751561208295527719']/div[1]/div[4]/div/table/tbody/tr[1]/td[1]/a"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
//jse.executeScript("window.scrollBy(0,500)", "");
jse.executeScript("arguments[0].scrollIntoView(true);", link);
link.click();
}
}

Thanks :)

How to scroll in Appium: click here

8 comments:

  1. Really cool post, highly informative and professionally written and I am glad to be a visitor of this perfect blog, thank you for this rare info!

    software testing training in chennai

    ReplyDelete
    Replies
    1. I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts. Python Projects for Students Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account. Project Center in Chennai

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Wonderful post!!Thank you for sharing this info with us.
    Keep updating I would like to know more updates on this topic
    Very useful content, I would like to suggest this blog to my friends.

    Best selenium Training Institute in Chennai

    best selenium training in chennai

    ReplyDelete
  4. I think this is the best article today. Thanks for taking your own time to discuss this topic, I feel happy about that curiosity has increased to learn more about this topic. Keep sharing your information regularly for my future reference.
    Software Testing Training in Chennai

    ReplyDelete
  5. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
    Web Design Training

    ReplyDelete
  6.  If someone week i really ashen-haired not actually pretty, whether you will lite grope a present, thought to follow us to displays bursting with ends of the earth considerably? Inside the impeccant previous, sea ever have dried-up, my hubby and i only may very well be with all of you connected thousands of samsara. https://serverbrowse.com/

    ReplyDelete