Play with scroll bars on a page in selenium and javaScript

There are some situations in which we want to scroll to a particular element or scroll up or down across the browser.

Selenium do not have it's own methods to perform this action, so we can use javascript to play on scroll lbars.

Let's see some of the ways:


public class Test1 {

 public static void main(String[] args) {
  WebDriver driver;
  driver = new FirefoxDriver();
  driver.get("http://www.qavalidation.com/p/demo.html");
  
  JavascriptExecutor js = (JavascriptExecutor) driver;
  //Scroll page down to 250px
  js.executeScript("window.scrollBy(0,250)", "");
  //or
  //js.executeScript("scroll(0,250)");
  
  //Scroll page to view an element 
  WebElement element = null;
  element = (WebElement) js.executeScript("return document.getElementById('table01');");
  js.executeScript("arguments[0].scrollIntoView(true);",element);
 }
}



16 comments:

  1. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.



    Manual testing training in Chennai

    Selenium training in Chennai

    Software testing training in Chennai

    ReplyDelete
  2. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    Data Science Training in Chennai
    Data science training in bangalore
    Data science online training
    Data science training in pune
    Data science training in kalyan nagar

    ReplyDelete
  3. Thank you so much for a well written, easy to understand article on this. It can get really confusing when trying to explain it – but you did a great job. Thank you!

    java training in chennai | java training in bangalore

    java training in tambaram | java training in velachery

    java training in omr | oracle training in chennai

    ReplyDelete

  4. Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.

    AWS Online Training | Online AWS Certification Course - Gangboard
    AWS Training in Chennai | AWS Training Institute in Chennai Velachery, Tambaram, OMR
    AWS Training in Bangalore |Best AWS Training Institute in BTM ,Marathahalli

    ReplyDelete
  5. A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article. I am learning a lot from you.

    python training in tambaram
    python training in annanagar
    python training in jayanagar

    ReplyDelete
  6. A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article. I am learning a lot from you.

    rpa training in electronic-city | rpa training in btm | rpa training in marathahalli | rpa training in pune

    ReplyDelete
  7. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information. 
    apple iphone service center in chennai | apple ipad service center in chennai | apple iphone service center in chennai | iphone service center in chennai |

    ReplyDelete
  8. Pretty blog, so many ideas in a single site, thanks for the informative article, keep updating more article
    Devops Training in Chennai | Devops Training Institute in Chennai

    ReplyDelete
  9. Today Telugu news updates provide us the information of breaking news and live updates. we get live news, political, education, technology, etc. Today Telugu news gives the best news updates. It also keeps its readers informed about the latest happenings in the world with instant updates.

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete