3

I would like to ask you how can I scroll into a web page by using a JS command. Though, I want this JS command inside a Java code-block. For the above purpose let me tell you that I also use Selenium framework. Is it possible?

The purpose is to do so by using Selenium and Java. I don't care about the general usage of JavascriptExecutor. I want their combination for SCROLL functionality!

  • 1
    Browsers cannot natively run Java. You will need some sort of Applet or Java plugin. If you are talking in context of a Java based web app using JSP, then it's possible by using JavaScript in JSP. – Sid Feb 06 '19 at 06:14
  • Possible duplicate of [JavaScript Executor in Selenium WebDriver](https://stackoverflow.com/questions/24098520/javascript-executor-in-selenium-webdriver) – Sercan Paspal Feb 06 '19 at 06:25
  • What do you mean by scroll? Scroll how much? Till the end of the page? Till a specific width? Till a specific element? Can you please specify this? – dpapadopoulos Feb 06 '19 at 06:59
  • @SercanPaspal what you tagged above is not referring to scroll option. Though is referring to JavascriptExecutor. The last is just a part of this question, not the question itself. That's my personal opinion. – dpapadopoulos Feb 06 '19 at 07:50
  • @Sid he said about Selenium so I guess that what he is looking for in my answer below. It's just a JavascriptExecutor interaction with Java. – dpapadopoulos Feb 06 '19 at 07:58
  • @DimitriosV.Papadopoulos Totally understand it. The question was edited to add Selenium after my comment. – Sid Feb 06 '19 at 08:34
  • @Sid my bad. I didn't know that. – dpapadopoulos Feb 06 '19 at 08:37

2 Answers2

3

If I understand you correctly you are trying to scroll the page inside your selenium code. You can try something like this.

WebDriver driver = new ChromeDriver();

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,1000)"); //Scroll vertically down by 1000 pixels

Let us know how this works for you.

1

I would like to answer in detail with all the possible scroll options because it is not determined in the question. So I give you below a practical and a theoretical answer. The first one to finish your job and the second one to read theory for future usages because there are many options about this theme.

Code sample:

import org.openqa.selenium.JavascriptExecutor; // packet that you need to import

WebDriver driver = new ChromeDriver(); // driver creation

JavascriptExecutor js = (JavascriptExecutor) driver; // giving to your driver the possibility to execute JS commands

js.executeScript("window.scrollBy(2000,1000)", ""); // scroll 2000 for x-coord and 1000 for y-coord
js.executeScript("window.scrollByPages(4)", ""); // scroll down the document by 4 pages
js.executeScript("window.scrollByPages(-4)", ""); // scroll up the document by 4 pages
js.executeScript("window.scrollByLines(10)", ""); // scroll down the document by 10 lines

WebElement toScrollElement = driver.findElement(By.XPATH_OR_ID_OR_OTHER("GIVEN_XPATH_OR_ID_OR_OTHER")); // locate the element you want to scroll into
js.executeScript("arguments[0].scrollIntoView(true);", toScrollElement); // scroll until the given element

Theory documentation:

I for one believe that the official documentation about this theme is included in the next links: here for scroll with Window options and here for scrolling with Element options. I am sure that more documentation from other sources exists too. I just present you sources from which I took the information.

Also, JavascriptExecutor has a second option which is the js.executeAsyncScript();. You can also read the documentation of JavascriptExecutor itself from here.

halfer
  • 19,824
  • 17
  • 99
  • 186
dpapadopoulos
  • 1,834
  • 5
  • 23
  • 34