Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from selenium import webdriver
|
| 2 |
+
from selenium.webdriver.common.by import By
|
| 3 |
+
from selenium.webdriver.common.keys import Keys
|
| 4 |
+
import time
|
| 5 |
+
from time import sleep
|
| 6 |
+
|
| 7 |
+
def main():
|
| 8 |
+
# Set up Facebook login account name and password
|
| 9 |
+
account = "sample@gmail.com"
|
| 10 |
+
password = "sample"
|
| 11 |
+
|
| 12 |
+
# Set up Facebook groups to post, you must be a member of the group
|
| 13 |
+
groups_links_list = [
|
| 14 |
+
"https://www.facebook.com/groups/sample1", "https://www.facebook.com/groups/sample2"
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
# Set up text content to post
|
| 18 |
+
message = "Checkout an amazing selenium script for posting automaticaaly on Facebook groups! https://github.com/ethanXWL/Python-Selenium-Facebook-group-poster"
|
| 19 |
+
|
| 20 |
+
# Set up paths of images to post
|
| 21 |
+
images_list = ['C:/Users/OEM/Pictures/sample1.jpg','C:/Users/OEM/Pictures/sample2.jpg']
|
| 22 |
+
|
| 23 |
+
# Login Facebook
|
| 24 |
+
chrome_options = webdriver.ChromeOptions()
|
| 25 |
+
prefs = {"profile.default_content_setting_values.notifications" : 2}
|
| 26 |
+
chrome_options.add_experimental_option("prefs",prefs)
|
| 27 |
+
driver = webdriver.Chrome(chrome_options=chrome_options)
|
| 28 |
+
driver.get('https://www.facebook.com')
|
| 29 |
+
emailelement = driver.find_element(By.XPATH,'//*[@id="email"]')
|
| 30 |
+
emailelement.send_keys(account)
|
| 31 |
+
passelement = driver.find_element(By.XPATH,'//*[@id="pass"]')
|
| 32 |
+
passelement.send_keys(password)
|
| 33 |
+
loginelement = driver.find_element(By.XPATH,'//*[@id="loginbutton"]')
|
| 34 |
+
loginelement.click()
|
| 35 |
+
|
| 36 |
+
# Post on each group
|
| 37 |
+
for group in groups_links_list:
|
| 38 |
+
driver.get(group)
|
| 39 |
+
time.sleep(2)
|
| 40 |
+
try:
|
| 41 |
+
driver.find_element(By.XPATH,'//*[@label="Start Discussion"]').click()
|
| 42 |
+
post_box=driver.find_element_by_css_selector("[name='xhpc_message_text']")
|
| 43 |
+
except:
|
| 44 |
+
post_box=driver.find_element_by_css_selector("[name='xhpc_message_text']")
|
| 45 |
+
post_box.send_keys(message)
|
| 46 |
+
time.sleep(1)
|
| 47 |
+
for photo in images_list:
|
| 48 |
+
photo_element = driver.find_element(By.XPATH,'//input[@type="file"]')
|
| 49 |
+
photo_element.send_keys(photo)
|
| 50 |
+
time.sleep(1)
|
| 51 |
+
time.sleep(6)
|
| 52 |
+
post_button = driver.find_element_by_xpath("//*[@data-testid='react-composer-post-button']")
|
| 53 |
+
clickable = False
|
| 54 |
+
while not clickable:
|
| 55 |
+
cursor = post_button.find_element_by_tag_name('span').value_of_css_property("cursor")
|
| 56 |
+
if cursor == "pointer":
|
| 57 |
+
clickable = True
|
| 58 |
+
break
|
| 59 |
+
post_button.click()
|
| 60 |
+
time.sleep(5)
|
| 61 |
+
# Close driver
|
| 62 |
+
driver.close()
|
| 63 |
+
|
| 64 |
+
if __name__ == '__main__':
|
| 65 |
+
main()
|