1

Hello Stackoverflow Users!

On many internet sites, there are some scrollable div containers. For example on facebook, if you click on any event and would like to know who is taking part in this event. You click on the button, it opens a list and you can see all the people. Another example, if you visit any instagram profile and click on "followers". Then it opens again a list and you can see all followers. These lists can be very long and a trick, to scroll all the list down, is press the "space" key on the keyboard.

My Question:

1.) How can I give Selenium the command, to press the "space" key for x seconds?

or

2.) How can I scroll these lists (not the browser site[only the div container]) automatically to the bottom?

I tried:

driver.FindElement(By.XPath("//div[contains(.,'followers')]")).SendKeys(Keys.Space);

I use:

Selenium, C#, chromedriver

Paski7
  • 41
  • 1
  • 6
  • Possible duplicate of [How to scroll to element with Selenium WebDriver using C#](https://stackoverflow.com/questions/28473710/how-to-scroll-to-element-with-selenium-webdriver-using-c-sharp) – Yahya Hussein Dec 29 '17 at 18:35
  • @YahyaHussein, no because I want to know, how can I give Selenium the command, to send any Key. And the second question ist, how to scroll only the div container, not the browser site. But Thanks anyway! – Paski7 Dec 29 '17 at 18:55
  • Possible duplicate of [How to scroll a specific DIV using Selenium WebDriver with Java?](https://stackoverflow.com/questions/27189182/how-to-scroll-a-specific-div-using-selenium-webdriver-with-java) While you mention sending space, that is a potential solution to your problem that might not be the best approach. – mrfreester Dec 29 '17 at 19:42

4 Answers4

1

KeyDown(): This method simulates a keyboard action when a specific keyboard key needs to press.

KeyUp(): The keyboard key which presses using the KeyDown() method, doesn’t get released automatically, so keyUp() method is used to release the key explicitly.

Actions action = new Actions(driver);
action.KeyDown(Keys.Space);
System.Threading.Thread.Sleep(1000); // trying to press space for X= 1000 ms 
action.KeyUp(Keys.Space);
action.Build().Perform();
AmitKS
  • 132
  • 4
0

This one IS pretty tricky...I have a partial answer, at best.

The best thing I can come up with is a loop to keep sending the space bar, since the keyDown command is only for certain keys (as near as I can tell from the documentation). Since the number of links will keep growing until you reach the end of the list, that's one way to figure out if you've hit bottom that isn't relying on the actual Y position.

        bool atBottom = false;

        while (!atBottom)
        {
            var linkNumber = driver.FindElementsByClassName("_6e4x5").Count;

            driver.FindElementByClassName("_6e4x5").SendKeys(Keys.Space); 

            var newLinkNumber = driver.FindElementsByClassName("_6e4x5").Count;

            atBottom = newLinkNumber.Equals(linkNumber);
        }

BUT I can't for the life of me get the web driver to focus on the element in order to send keys. I've tried multiple class names and the xpath...it's almost like IG didn't want this automated.

You might find this Stack question helpful, as calling JavaScript directly may serve you well as a workaround.

jdmac020
  • 557
  • 1
  • 5
  • 18
0

you have the LocationOnScreenOnceScrolledIntoView in selenium. You can use it to scroll the div you want into the view with this:

        public static IWebElement FindElementOnPageScrolling(this IWebDriver driver, By element)
    {
        RemoteWebElement remoteElement = (RemoteWebElement)driver.FindElement(element);
        var foo = remoteElement.LocationOnScreenOnceScrolledIntoView;
        return remoteElement;
    }

This usually works well in chrome and firefox, in IE and Edge it won't work well, at least in my experience.

And you can try also to send Key.Down or Key.End to scroll down if you have not try this.

Gsanez
  • 61
  • 6
  • [please provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead) – Tim Diekmann Jun 04 '18 at 13:08
0

For the fist one try to use ClickAndHold(IWebElement element) method which part of the Actions class. For the second one you can try to use JS command ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView();",element); You will need to locate the last element in the list and pass it as argument to the JS function.