I've made a Facebook scraper using VBA in combination with selenium which is able to parse the name from friend request list.

It is necessary to do one thing manually, though. After running this script, when Facebook page opens up, there will be a notification popping up mentioning "Allow" or "Block". Wait a little while to get the buffering done and then Just click on "Allow". That's it.

There is a javascript function within this script to handle scrolling the webpage.

Here is the working code which I have tried to make accurately:

Sub Facebook_Crawler()

Dim driver As New WebDriver
Dim posts As Object, post As Object

With driver
    .Start "chrome", "https://www.facebook.com"
    .get "/login.php?login_attempt=1&lwv=111"

    .FindElementByXPath("//*[@id='email']").SendKeys ("someEmail") --Fill in the email
    .FindElementByXPath("//*[@id='pass']").SendKeys ("password") --Fill in the password
    .Wait 800
    .Wait 500
End With

For x = 0 To 50
    driver.ExecuteScript "window.scrollTo(0, document.body.scrollHeight);"
    driver.Wait 500
Next x

Set posts = driver.FindElementsByXPath("//div[contains(@class,'friendBrowserNameTitle')]/a")

For Each post In posts
    i = i + 1
    Cells(i, 1) = post.Text
Next post

End Sub

1 Answer 1


VBAs selenium bindings are not as rich in functionality as other language bindings, but I would improve the following:

  • use the "by id" locators whenever possible:

    .FindElementById("email").SendKeys ("someEmail") --Fill in the email
    .FindElementById("pass").SendKeys ("password") --Fill in the password
  • if tempting to use XPaths, give CSS selectors a chance:

    Set posts = driver.FindElementsByCss(".friendBrowserNameTitle > a")
  • avoid "magic" numbers in the code, like that value 50 - the number of scroll actions - configure it as a constant
  • I also don't particularly like the .Wait calls - this is typically a red flag when it comes to browser automation with selenium, but I am not sure VB bindings provide an Explicit Wait with a pre-defined condition
  • \$\begingroup\$ Thanks sir alecxe, for your suggestion. I will keep that in mind and abide by. \$\endgroup\$
    – SIM
    Commented Jul 7, 2017 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.