All the lines of codes are easily relatable as we have discussed in our previous example.
However, in this program, we have included our JavascriptExecutor js which will do the scrolling. If you see the last line of the code, we have passed window.scrollBy(arg1,arg2).
If we want to scroll up then pass some value in arg1 if you want to scroll down then pass some value in arg2.
3 | import java.util.concurrent.TimeUnit; |
5 | import org.openqa.selenium.By; |
6 | import org.openqa.selenium.JavascriptExecutor; |
7 | import org.openqa.selenium.Keys; |
8 | import org.openqa.selenium.WebDriver; |
9 | import org.openqa.selenium.WebElement; |
10 | import org.openqa.selenium.chrome.ChromeDriver; |
12 | public class ScrollDown { |
14 | public static void main(String[] args) { |
16 | System.setProperty( "webdriver.chrome.driver" , "C:\\webdriver\\chromedriver.exe" ); |
17 | WebDriver driver = new ChromeDriver(); |
18 | JavascriptExecutor js = (JavascriptExecutor) driver; |
19 | driver.manage().window().maximize(); |
20 | driver.manage().timeouts().implicitlyWait( 20 , TimeUnit.SECONDS); |
21 | driver.get( "https://www.google.com" ); |
22 | WebElement element = driver.findElement(By.name( "q" )); |
23 | element.sendKeys( "SoftwareTestingHelp" ); |
24 | element.sendKeys(Keys.ENTER); |
25 | js.executeScript( "window.scrollBy(0,1000)" ); |