-1

I've got a problem, because I need to scroll down the page, so I decided to use IJavaScriptExecutor

IJavaScriptExecutor js =  (IJavaScriptExecutor) driver;
js.executeScript("window.scrollBy(0,1000)"); 

I also tried

js.ExecuteScript("arguments[0].scrollIntoView();", invite); 

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

But it doesn't work.

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

1 Answers1

1

Before you attempt to scroll down a page ensure that the DOM Tree have rendered completely. Here you can find a detailed discussion How can I make sure if some HTML elements are loaded for Selenium + Python?

Once the Page Load is completed you can invoke the scroll() methods as follows:

  • To scroll down 250 pixcels:

    IJavaScriptExecutor js = (IJavaScriptExecutor) driver;
    js.ExecuteScript("window.scrollBy(0,250)", "");
    
  • To scroll down the entire page:

    IJavaScriptExecutor js = (IJavaScriptExecutor) driver;
    js.ExecuteScript("window.scrollTo(0, document.body.scrollHeight)");
    
  • To scroll an element into view:

    IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.CssSelector("css_element")));
    IJavaScriptExecutor js = (IJavaScriptExecutor) driver;
    js.ExecuteScript("arguments[0].scrollIntoView(true);",element);
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • So, I need to make a method, for example which will wait for example 30 seconds to make a part of site's head visible, and then invoke the scroll() method? Right? – Mariusz Budzisz Oct 22 '18 at 13:56
  • @MariuszBudzisz You don't need a function but a simple instance of WebDriverWait. That would do. – undetected Selenium Oct 22 '18 at 13:58
  • public void Invite(){ var span = new TimeSpan(0, 0, 0, 60, 0); var wait = new WebDriverWait(driver, span); wait.Until(foo => foo.FindElement(By.CssSelector("#seo_h1_tag > a:nth-child(1)")).Enabled); js.ExecuteScript("window.scrollBy(0,250)", ""); wait.Until(foo => foo.FindElement(By.CssSelector("button._42ft._4jy0.FriendRequestAdd.addButton._4jy3._517h._51sy")).Enabled); IWebElement invite = driver.FindElement(By.CssSelector("button._42ft._4jy0.FriendRequestAdd.addButton._4jy3._517h._51sy")); invite.Submit(); invite.SendKeys(Keys.Return); } Dont work. – Mariusz Budzisz Oct 22 '18 at 14:23
  • @MariuszBudzisz I don't have a visibility about `("#seo_h1_tag > a:nth-child(1)")`. What are your exact _Manual Steps_ which you are trying to _Automate_? – undetected Selenium Oct 22 '18 at 14:27
  • 1-> go to Yagiellonian University facebook fanpage https://www.facebook.com/jagiellonian.university/ ("#seo_h1_tag > a:nth-child(1)") is a logo of UJ, placed at the top of fanpage 2-> scroll down (don't work) 3-> push the "Add friend" button several times – Mariusz Budzisz Oct 22 '18 at 14:29
  • I am afraid :( Scrapping [_Facebook_](https://www.facebook.com/) is against the [_ToS 3.2_](https://www.facebook.com/legal/terms) and you are liable to be questioned and may even land up in [_Facebook Jail_](https://www.facebook.com/help/community/question/?id=804287426255468). Use [**`Facebook Graph API`**](https://developers.facebook.com/docs/graph-api) instead. – undetected Selenium Oct 22 '18 at 14:29
  • hah, thanks for info m8 :) Still, i just want to make a C# .NET Core 2.0 version of this: https://pythonicways.wordpress.com/2016/11/02/add-friends-on-facebook-python/ – Mariusz Budzisz Oct 22 '18 at 14:31
  • @MariuszBudzisz Shouldn't that be a separate question all together? Feel free to raise a new questionn with your new requirement. StackOverflow contributors will be happy to help you out. – undetected Selenium Oct 22 '18 at 15:12