Currently I am trying to build a FB bot that can post a string of text and a video url to Facebook groups that I am a part of. I am helping a family member with this so that their workflow can be automated.
My problem is: I got the code using selenium to login and go to the groups that I am a part of, however it cannot find the element on the page for the post box where I need to pass in my string of text and video url. I tried by copying the full xpath but it cannot find that element. Please let me know if there are any ideas, this has been driving me crazy!
Here is the error I am getting: The error I am getting is from this line of code (line 51):
post_box=driver.find_element_by_xpath("/html/body/div[1]/div[3]/div[1]/div/div[2]/div[2]/div[2]/div[2]/div[3]/div[1]/div/div/div[2]/div[1]/div/div/div/div[2]/div/div[1]/div/div[1]/div[1]/div[2]/div/div/div/div/div/div/div[2]/div/div/div/div")
Error message: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
How could it not locate the element if I passed in the full xpath?
Here is my code:
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def main():
# Your Facebook account user and password
usr = "****"
pwd = "****"
message = "Checkout an amazing selenium script for posting in Facebook Groups!\nhttps://github.com/lalongooo/selenium-fb-group-poster"
attach_video = True
video_url = "https://www.youtube.com/watch?v=70UXjyy6K84"
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("detach", True)
chrome_options.add_argument("--disable-infobars")
chrome_options.add_experimental_option("prefs", { \
"profile.default_content_setting_values.notifications": 2 # 1:allow, 2:block
})
driver = webdriver.Chrome(options=chrome_options)
driver.implicitly_wait(15) # seconds
# Go to facebook.com
driver.get("http://www.facebook.com")
# Enter user email
elem = driver.find_element_by_id("email")
elem.send_keys(usr)
# Enter user password
elem = driver.find_element_by_id("pass")
elem.send_keys(pwd)
# Login
elem.send_keys(Keys.RETURN)
groups = ['3069593553138733', '345263603157484']
time.sleep(5)
for i in range(len(groups)):
# Navigate to group url
driver.get('https://facebook.com/groups/'+groups[i])
# Click on the write something box
post_box=driver.find_element_by_xpath("/html/body/div[1]/div[3]/div[1]/div/div[2]/div[2]/div[2]/div[2]/div[3]/div[1]/div/div/div[2]/div[1]/div/div/div/div[2]/div/div[1]/div/div[1]/div[1]/div[2]/div/div/div/div/div/div/div[2]/div/div/div/div")
post_box.click()
# Enter the text we want to post to Facebook
post_box.send_keys(message)
post_button = driver.find_element_by_xpath("/html/body/div[1]/div[3]/div[1]/div/div[2]/div[2]/div[2]/div[2]/div[3]/div[1]/div/div/div[2]/div[1]/div/div/div/div[2]/div/div[3]/div[3]/div/div[2]/div/div[2]/button/span")
clickable = False
while not clickable:
cursor = post_button.find_element_by_tag_name('span').value_of_css_property("cursor")
if cursor == "pointer":
clickable = True
break
post_button.click()
sleep(5)
# driver.close()
if __name__ == '__main__':
main()