Facebook Posting Using Selenium and C# (.NET Core)
In this article, I will guide you through the process of automating the creation of a new Facebook post using Selenium and C# via .NET Core 3.1.
First off, why would you even want to do that?
Well, there are some scenarios that might come in handy:
For example — let's say you have software that generates some sort of textual content on a schedule (stock analysis? weather statistics? product price tracking?), and you want to share that info/analysis with your Facebook friends, or with your Facebook group automatically.
We will build upon the project we already set up in my previous article, and extend the “FacebookAutomation” class with additional functionality.
First off, let’s add the following method to the FacebookAutomation class:
This method will allow us to implement a “wait” block until a condition is met.
This methodology is superior to an arbitrary “sleep” period, as it will only wait for the minimal time required, and move on to the next automation step right after such condition is met.
The problem with “Arbitrary” sleep statements, is that they are unpredictable — you don’t really know how long you will need to wait until Facebook completes the form validation or until a pop up opens or closes, so you end up using sleep durations that are too long, making the execution of the automation scenario unnecessarily slow.
You will see how to invoke this method in the new method we will be adding next to the “FacebookAutomation” class:
You can see that we are “finding” the post text box, typing in the value into it, and then we wait until the post button is enabled and ready to be clicked on.
Next, we’ll click on the button (using the “ClickAndWaitForPageToLoad” method that already exists in our class — see the previous article) and wait until the submission process is complete before moving on and leaving the method.
Let's tie it all together and call this method in our Main method:
A word of caution — Facebook is cracking down on what it believes to be spam.
Don’t flood Facebook with meaningless content that is obviously computer generated and provide little value to the platform's users.
From my experience, Facebook will usually take action if your posts are reported as spam by other users, but no one really knows how their algorithms work to identify such content automatically (if at all).