0

Hey All I have a question regarding selenium I have a very long form, and I want to scroll a little bit down for entering data. I do not want it to scroll all the way just until the element is displayed.

I used this code:

WebElement element = driver.findElement(locator);
        JavascriptExecutor js = (JavascriptExecutor) driver; 
js.executeScript("arguments[0].scrollIntoView();", element);

It not worked since selenium scroll to match down, is their a way it will scroll to this element so I Can enter data to it, for example to scroll until element is in the moddle of screen. I do not know what is the purpose of scroll into view if it is scrolling not to view regards

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Bastian
  • 1,089
  • 7
  • 25
  • 74
  • Shouldn't Selenium scroll to the field, if it is outside of the viewport, when you are trying to send keys to it? – Jonah Jan 10 '20 at 09:55

3 Answers3

2

If your usecase is to ...scroll a little bit down for entering data... you have multiple ways to achieve that:

  • You can induce WebDriverWait for the elementToBeClickable() which will automatically scroll the desired element within the Viewport as follows:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(driver.findElement(locator))).sendKeys("Bastian");
    
  • You can use Actions class method moveToElement() inconjunction with sendKeys() as well which will also automatically scroll the desired element within the Viewport as follows:

    new Actions(driver).moveToElement(driver.findElement(locator)).sendKeys("Bastian").build().perform();
    
  • You can still use scrollIntoView() method to scroll the element first and then induce WebDriverWait for the elementToBeClickable() as follows:

    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", driver.findElement(locator));
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(driver.findElement(locator))).sendKeys("Bastian");
    
  • You can also use scrollBy() method to scroll down certain amount of xy-coord (adjusted) as follows and then locate the element:

    ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,400)");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(driver.findElement(locator))).sendKeys("Bastian");
    

References

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • The issue is not to wait, It can be wait for hours since the element I want to click is not display. and the scroll takes me to much down and still not see the element. I need selenium to stop scroll at the point the actually can do something press element click on it and so – Bastian Jan 10 '20 at 10:23
  • @Bastian Checkout the updated answer and let me know the status. – undetected Selenium Jan 10 '20 at 10:27
0

Please try with this. It works for me.

 public void scrollElementToCenter(WebElement element) {

        try {
            if (EnvironmentConstants.RUNNING_ON_CHROME) {
                ((JavascriptExecutor) driver)
                        .executeScript("arguments[0].scrollIntoView({behavior: \"auto\", block: \"center\", inline: \"nearest\"});", element);
            } else {
                ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(false);", element);
            }
        } catch (UnsupportedOperationException exception) {
            //LOGGER.error("UnsupportedOperationException occurred : " + exception.getMessage(), exception);
        }
    }

for Chrome browser you can use it

((JavascriptExecutor) driver)
                        .executeScript("arguments[0].scrollIntoView({behavior: \"auto\", block: \"center\", inline: \"nearest\"});", element);
Muzzamil
  • 2,823
  • 2
  • 11
  • 23
  • Can you please be more specific, I Need the function only to chrome. another question if I want that the method will get By locator as an input what to change? – Bastian Jan 10 '20 at 15:05
  • @Bastian, I have mentioned specially for chrome, Please try that piece of code. You need to pass only webelement in this : ((JavascriptExecutor) driver) .executeScript("arguments[0].scrollIntoView({behavior: \"auto\", block: \"center\", inline: \"nearest\"});", webElement); – Muzzamil Jan 10 '20 at 15:08
0

This is what I created and seems to be working for me.

for i in range(5000):
    browse = "window.scrollTo(0," + str(i) + ")"
    browser.execute_script(browse)
    i = i + 400

It pretty much scrolls slowly so every element on the page is loaded.

Dharman
  • 30,962
  • 25
  • 85
  • 135