Wednesday, June 2, 2021

How to post or share to multiple facebook groups using python and selenium using xpath

from selenium import webdriver import traceback from time import sleep from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.by import By from bs4 import BeautifulSoup as bs from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import re sites=["https://www.facebook.com/groups/813215445837439/", "https://www.facebook.com/groups/1477663489136837/", "https://www.facebook.com/groups/1094411787306299/", "https://www.facebook.com/groups/191633844725018/", "https://www.facebook.com/groups/salesforcehyd/", "https://www.facebook.com/groups/167528139970828/", "https://www.facebook.com/groups/479617975381544/", "https://www.facebook.com/groups/309645370012078/", "https://www.facebook.com/groups/199321180678412/", "https://www.facebook.com/groups/oraclesiebeleim/", "https://www.facebook.com/groups/computer.science.and.software.engineering/", "https://www.facebook.com/groups/2665006127118033/", "https://www.facebook.com/groups/340148747290497/" ] ###################### try: yy = 0 zz = 0 usr='' pwd='' option = Options() option.add_experimental_option("prefs", { "profile.default_content_setting_values.notifications": 1 }) option.add_argument("window-size=480x854") driver = webdriver.Chrome(chrome_options=option) driver.execute_script("document.body.style.zoom = '0.50'") driver.get('https://www.facebook.com/') #print ("Opened facebook") sleep(5) username_box = driver.find_element_by_id('email') username_box.send_keys(usr) #print ("Email Id entered") sleep(1) password_box = driver.find_element_by_id('pass') password_box.send_keys(pwd) #print ("Password entered") sleep(2) login_box = driver.find_element_by_class_name('_42ft._4jy0._6lth._4jy6._4jy1.selected._51sy') login_box.click() sleep(3) driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't') sleep(1) #driver.set_window_size(50, 50) sleep(10) print(sites) for z in sites: try: driver.get(sites[zz]) zz = zz + 1 sleep(10) a = driver.find_element_by_class_name('bp9cbjyn.j83agx80.ihqw7lf3.hv4rvrfc.dati1w0a.pybr56ya') print(a) driver.implicitly_wait(10) ActionChains(driver).move_to_element(a).perform() a.click() ele3 = driver.switch_to.active_element #find this xpath from inspect element ,rght click full xpath b = driver.find_element_by_xpath("/html/body/div[1]/div/div[1]/div/div[4]/div/div/div[1]/div/div[2]/div/div/div/div/div[1]/form/div/div[1]/div/div/div[1]/div[2]/div[1]/div[1]/div[1]/div/div/div/div/div/div/div/div/div") sleep(5) b.send_keys('facbook posting') c =driver.find_element_by_xpath("/html/body/div[1]/div/div[1]/div/div[4]/div/div/div[1]/div/div[2]/div/div/div/div/div[1]/form/div/div[1]/div/div/div[1]/div[3]/div[2]/div[1]/div") #ActionChains(driver).move_to_element(c).perform() c.click() sleep(20) #b.send_keys('testing the bot2',Keys.ENTER) #c =driver.find_element_by_xpath("/html/body/div[1]/div/div[1]/div/div[4]/div/div/div[1]/div/div[2]/div/div/div/div/div[1]/form/div/div[1]/div/div/div[1]/div[3]/div[2]/div[1]/div") #ActionChains(driver).move_to_element(c).perform() #c.click( except Exception as exception: print(traceback.format_exc()) continue sleep(3) sleep (2) #print (elem2) print ("Done") #input('Press anything to quit') driver.quit() print("Finished") exit() except Exception as exception: print(traceback.format_exc()) exit()