javascriptselenium-webdriverselenium-ide

windows scroll is not working in selenium webdriver


I have to click at an element which is intercepted by other element, so what i'm doing is that, I'm trying to scroll down the Vertical scroll bar and try to click my target element, so to scroll this i'm using the below code

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollBy(0, -800)", "");

But the above code is not scrolling down the scroll bar

Note: i also tried to movetoelement. That is also not working, please help me with your solutions

action.moveToElement(seatLayout.seat_A16);
Thread.sleep(2000);
seatLayout.seat_A16.click();

Solution

  • To scroll down:

    ((JavascriptExecutor)driver).executeScript("window.scrollBy(0, 800)", "");
    

    To scroll up:

    ((JavascriptExecutor)driver).executeScript("window.scrollBy(0, -800)", "");
    

    To scroll the element within the viewport:

    ((JavascriptExecutor)driver).executeScript("return arguments[0].scrollIntoView(true);", element);
    

    References

    You can find a couple of relevant detailed discussions in: