Search code examples
javascriptjavaselenium-webdriverautomated-tests

JavascriptExecutor for scrolling does not work with selenium


This what I have tried

Thread.sleep(3000);
JavascriptExecutor js3 = (JavascriptExecutor) driver;
js3.executeScript("window.scrollBy(0,500)", "");

But the page does not scroll down, is there perhaps another way of scroll down on a form, because when I am trying the above the form won't scroll so I am not able to get to the below element.

I want to scroll down to this element on the form.

'<div class="ant-select-selection__rendered" style=""><div unselectable="on" class="ant-select-selection__placeholder" style="display: none; user-select: none;">Select...</div><div title="" class="ant-select-selection-selected-value" style="display: block; opacity: 1;"></div></div>'

Solution

  • You can try the below way. I extracted the locator from your given html make sure its unique.

    WebElement element = driver.findElement(By.cssSelector("div.ant-select-selection__rendered"));
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("arguments[0].scrollIntoView(true);", element);