Skip to content

Instantly share code, notes, and snippets.

@LucaSantosuosso
Last active November 15, 2021 23:38
Show Gist options
  • Save LucaSantosuosso/8b42953083e2aa649a29cf61d6b68489 to your computer and use it in GitHub Desktop.
Save LucaSantosuosso/8b42953083e2aa649a29cf61d6b68489 to your computer and use it in GitHub Desktop.
Selenium script to post in multiple facebook groups
from selenium import webdriver
#Parameters
email_txt = 'here the email' # FB credentials
pwd_txt = 'here the password' # FB credentials
message_txt = "here the message" #Message to post
photo_path_txt = 'here the photo path' #Photo to post
file_name = "namefile.txt" #URLs file
#To disable notifications
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
#Start Chrome
driver = webdriver.Chrome(chrome_options=chrome_options)
#Get the open url
driver.get('https://www.facebook.com/')
#Login
email = driver.find_element_by_id('email')
email.send_keys(email_txt)
pwd = driver.find_element_by_id('pass')
pwd.send_keys(pwd_txt)
login_btn = driver.find_element_by_id('loginbutton')
login_btn.submit()
#Open file with groups URLs
f = open(file_name, "r")
#Main cycle, change 3 to N to read N URLs from the file
for x in range(0, 3):
#Read the url
driver.get(f.readline())
#Add text to the post
post_box=driver.find_element_by_xpath("//*[@name='xhpc_message_text']")
post_box.click()
post_box.send_keys(message_txt)
#Add photo to the post
add_photo_btn=driver.find_element_by_xpath("//*[@containerclassname='_5g_r']")
add_photo_btn.send_keys(photo_path_txt)
#Post
post_btn=driver.find_element_by_xpath("//*[@data-testid='react-composer-post-button']")
post_btn.click()
@fmaor267
Copy link

fmaor267 commented May 7, 2020

the code cant see the ''filename'' at the folder ??

@Hakan010
Copy link

Hakan010 commented Nov 15, 2021

Hi Luca, What do you mean with file_name? Should this be the url of the group or something else? Also I would like you to recommend add the following to accept cookies: driver.find_element_by_xpath('//*[@Class="_42ft _4jy0 _9xo7 _4jy3 _4jy1 selected _51sy"]').click()

Woulld like to hear from you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment