0

enter image description here

Attached is the picture of the scroll bar that I would like to move.

I already have a method that is not working. could you please tell me what's wrong?

    public static void ScrollPageToEnd(this IWebDriver Driver)
    {
        var maxScrollY = Driver.Scripts().ExecuteScript("return document.documentElement.scrollHeight;");
        Driver.Scripts().ExecuteScript("window.scrollTo(0," + maxScrollY + ")");        

    }
user2320213
  • 3
  • 1
  • 9

4 Answers4

0

It is possible that var maxScrollY is not a string, (ExecuteScript returns object from what I can recall). Try to save the object ExecuteScriptto a string with string maxScrollY = Driver.Scripts().ExecuteScript("return document.documentElement.scrollHeight;").ToString().

Also, check with debug the object's value and see what window.scrollTo(0," + maxScrollY + ") equals to.

  • 1
    You're right that `IJavaScriptExecutor.ExecuteScript` returns an object, but concatenating a string and an object calls ToSring on that object, so I don't believe that that's the problem – Arnon Axelrod Aug 16 '18 at 06:17
0

Use scrollIntoView function of JavascriptExecutor interface to scroll to that location where the Image is.

WebElement element = driver.findElement("locator of the image")
JavascriptExector js = (JavascriptExecutor)driver;
js.executeScript("argument[0].scrollIntoView");",element)

or

we can use method Scroll(x,y) to scroll to particular location

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

it is working for me:

((IJavaScriptExecutor)Driver).ExecuteScript("window.scrollTo(0, document.body.scrollHeight)");

i use it as a function to be easier:

public void ScrollToEndOfThePage()
        {
            ((IJavaScriptExecutor)Driver).ExecuteScript("window.scrollTo(0, document.body.scrollHeight)");
        }
Mahsum Akbas
  • 1,523
  • 3
  • 21
  • 38
0

if any JS script is not helping you to scroll, then try MoveToElement?

IWebElement element = driver.FindElement(By.Id("yourLookingId"));
new Actions(driver).MoveToElement(element).Perform(); 

I am using this in a complicated web application with scroll bar and it works perfectly.

martin pb
  • 105
  • 1
  • 1
  • 6