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

1
  • Shouldn't Selenium scroll to the field, if it is outside of the viewport, when you are trying to send keys to it? Commented Jan 10, 2020 at 9:55

3 Answers 3

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:

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

2 Comments

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 Checkout the updated answer and let me know the status.
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);

2 Comments

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, 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);
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.

Comments

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.