forked from Samihan15/python-bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacebook_post_creation.py
54 lines (41 loc) · 1.71 KB
/
facebook_post_creation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
usn = "[email protected]"
pas = "######"
txt = "Hello world"
chrome_options = Options()
chrome_options.add_argument("--disable-notifications")
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.facebook.com/login/")
driver.maximize_window()
username = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "email")))
password = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "pass")))
login_btn = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "loginbutton")))
username.send_keys(usn)
password.send_keys(pas)
login_btn.click()
time.sleep(5)
try:
WebDriverWait(driver, 10).until(EC.alert_is_present())
alert = driver.switch_to.alert
alert.dismiss()
except Exception as e:
pass
post = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//span[contains(text(),"What\'s on your mind, Samihan?")]'))
)
driver.execute_script("arguments[0].scrollIntoView(true);", post)
time.sleep(1)
driver.execute_script("arguments[0].click();", post)
post_text = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//div[@role="textbox"]')))
post_text.send_keys(txt)
# Correct the post_btn locator (use a tuple)
post_btn = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, "//span[contains(text(),'Post')]")))
post_btn.click()
time.sleep(10)
driver.quit()