Python Facebook Automation
By: Mohamed Zeghlache
Tags: Python, Automation, Facebook, Selenium

A Selenium Script to share Facebook status in multiple groups Automatically

Hello Everyone, in this post we will see how to Automate sharing posts in different groups on Facebook using Python and Selenium.

we will be using selenium and the chrome web Driver to get a Facebook post and then share it on multiple Facebook groups with only a couple of steps.

if you have not used the chrome web Driver before it will be installed for the first time(will take only a few seconds), but then it will not be installed each time it’s executed … it will get the setup from the cache. we will provide the link to the post that we want to share, we also need to provide the password and email of the Facebook account that we are using to share.

first of all, we are gonna need to import the following packages

in this script, we will provide the necessary information needed to login and get the Facebook post that we are interested in, and also a text file that contains a list of the groups that we will be sharing our post in.

we will start the ChromeWebDriver and get the Facebook login page and sign in using the provided information.

user_name.send_keys(email)

password=driver.find_element_by_name("pass")

password.send_keys(motDePass)

password.submit()

time.sleep(10) driver.get(fb_post)

Then, we will go through our groups' file and share in each one of them using the following code:

with open(groups_file, 'r', encoding='utf-8') as file:

for group in file:

try:

share = WebDriverWait(driver, 10).until(

EC.presence_of_element_located((By.XPATH, '//div[normalize-space()="Partager"]/div'))

)

share.click()

except:

print(group)

driver.get(fb_post)

time.sleep(10)

continue try:

shareToGroup = WebDriverWait(driver, 10).until(

EC.presence_of_element_located((By.XPATH, '//div[normalize-space()="Partager dans un groupe"]/div'))

)

time.sleep(5)

shareToGroup.click()

except:

print(group)

driver.get(fb_post)

time.sleep(10)

continue

group_name = WebDriverWait(driver, 10).until(

EC.presence_of_element_located((By.XPATH , "//input[

)

group_name.send_keys(group)

time.sleep(5)

except:

print(group)

driver.get(fb_post)

time.sleep(10)

continue try:group_name = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH , "//input[ @role ='textbox']"))group_name.send_keys(group)time.sleep(5)except:print(group)driver.get(fb_post)time.sleep(10)continue

click_group = WebDriverWait(driver, 10).until(

EC.presence_of_element_located((By.XPATH , "//div[

)

click_group.click()

except:

print(group)

driver.get(fb_post)

time.sleep(10)

continue try:click_group = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH , "//div[ @class ='glvd648r']//div[ @role ='button']"))click_group.click()except:print(group)driver.get(fb_post)time.sleep(10)continue

publish = WebDriverWait(driver, 10).until(

EC.presence_of_element_located((By.XPATH , "//div[normalize-space()='Publier']//div[

)

time.sleep(3)

publish.click()

except:

print(group)

driver.get(fb_post)

time.sleep(10)

continue try:publish = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH , "//div[normalize-space()='Publier']//div[ @role ='button']"))time.sleep(3)publish.click()except:print(group)driver.get(fb_post)time.sleep(10)continue time.sleep(10)

in the previous code, I am using time.sleep(10) which might seem a lot and our code will take a long time to execute.. the reason behind that is that my internet connection is horrible and I want to make sure my page is fully loaded before the script starts clicking any buttons

Also, if your google chrome is in English you might get an error using this script because it uses a couple of french words when looking for buttons ( words such as Publier = Publish)… because the browser I use at work is in french.

This is not the perfect script, it’s a quick solution that can be further developed.

You can find the whole script in my Github profile.

thank you

https://github.com/Mohamed-Zeghlache/Python-Facebook-Automation