4

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!

7
  • 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. Commented Feb 6, 2019 at 6:14
  • Possible duplicate of JavaScript Executor in Selenium WebDriver Commented Feb 6, 2019 at 6: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? Commented Feb 6, 2019 at 6: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. Commented Feb 6, 2019 at 7: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. Commented Feb 6, 2019 at 7:58

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

Thanks for your detailed answer. thats waht i want

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.