5
\$\begingroup\$

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
    .FindElementByXPath("//*[@id='loginbutton']").Click
    .Wait 800
    .FindElementByXPath("//a[@id='findFriendsNav']").Click
    .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
\$\endgroup\$
1
\$\begingroup\$

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
    .FindElementById("loginbutton").Click
    
  • 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
\$\endgroup\$
  • \$\begingroup\$ Thanks sir alecxe, for your suggestion. I will keep that in mind and abide by. \$\endgroup\$ – SIM Jul 7 '17 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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