2

I know this is a not a proper technical question, but i am facing problem while using selenium to make a facebook post bot. This is my code so far

    from selenium import webdriver

browser = webdriver.Firefox(executable_path='D:\\soft\\geckodriver-v0.18.0-win64\\geckodriver.exe')
browser.get('http://www.facebook.com')
emailElem = browser.find_element_by_id('email')
passElem = browser.find_element_by_id('pass')
submitIt = browser.find_element_by_id('u_0_r')
email_id = 'xxxxxxxxxxxxxx'
password = 'xxxxxxx'
status = 'Hie all'
emailElem.send_keys(email_id)
passElem.send_keys(password)
submitIt.click()
statusBox = browser.find_element_by_xpath(
   "//*[@id='js_kk']/div[1]/div/div[1]/div[2]/div/div/div/div/div/div[2]/div/div/div/div/span/br")
statusBox.send_keys("Hie")

Now, the problem is my code is not able to locate the status box in Fb page, I am quite new to selenium. Any help would be nice.It is coded in python.

Alpit Anand
  • 1,213
  • 3
  • 21
  • 37
  • 2
    Facebook’s ToS forbid using any such automation. If you want to post - use their API. – CBroe Aug 11 '17 at 12:25
  • 1
    I know, i am just beginner,trying to learn stuff. – Alpit Anand Aug 11 '17 at 12:27
  • Well then go learn the _right_ stuff ... – CBroe Aug 11 '17 at 12:28
  • Yes, You are right, sure i will. – Alpit Anand Aug 11 '17 at 12:31
  • 1
    @CBroe ------ :D – PySaad Aug 11 '17 at 13:28
  • 1
    @AlpitAnand There are many ways to "learn stuff". When your question states that you are writing a facebook post bot that directly violates facebook's ToS, that's not a good way to learn. There are plenty of other sites out there, some specifically created to learn automation with companion tutorials. I would start there. – JeffC Aug 11 '17 at 14:43
  • 1
    Thanks @JeffC i really appreciate it, my main aim was to do it on some complex websites,and as a teenager i selected facebook, However, i will keep in mind to read company policies. And yes sure, i will def give those tutorials a try, I was currently following book, Automate boring stuff with python – Alpit Anand Aug 11 '17 at 15:21

3 Answers3

1

Have you tried just finding the element by ID as that Xpath looks very likely to change?

driver.FindElement(By.Id("js_kk")).SendKeys(status);

1

Here is the sample code block to access the Facebook Login Page, login through a valid set of credentials and type in "Hie" in the Status Box using xpath as well as css_selector:


Using XPATH :

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("https://www.facebook.com/")
driver.find_element_by_xpath("//input[@id='email']").send_keys("email@domain.com")
driver.find_element_by_xpath("//input[@id='pass']").send_keys("password")
driver.find_element_by_xpath("//input[starts-with(@id, 'u_0_')][@value='Log In']").click()
print(driver.title)
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(@id, 'u_0_')]//textarea[@name='xhpc_message']")))
driver.find_element_by_xpath("//div[starts-with(@id, 'u_0_')]//textarea[@name='xhpc_message']").send_keys("Hie")
print("Typed Hie within Facebook Status Box")

Using CSS_SELECTOR :

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("https://www.facebook.com/")
driver.find_element_by_css_selector("input#email").send_keys("email@domain.com")
driver.find_element_by_css_selector("input#pass").send_keys("password")
driver.find_element_by_css_selector("input[id^='u_0_'][value='Log In']").click()
print(driver.title)
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[id^='u_0_'] textarea[name=xhpc_message]")))
driver.find_element_by_css_selector("div[id^='u_0_'] textarea[name=xhpc_message]").send_keys("Hie")
print("Typed Hie within Facebook Status Box")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

This Code is to log in to Facebook and Post "Hello World" text in status

public void facebookLogin() throws InterruptedException {

 //1. Set gecko driver path

System.setProperty("webdriver.gecko.driver","C:\\Selenium\\selenium-java-3.0.1\\geckodriver.exe");

WebDriver d= new FirefoxDriver();

  //2. Enter URL 

  d.get("https://www.facebook.com/");

  //3. maximize window

  d.manage().window().maximize();
  Thread.sleep(2000);

   //4. Login Into Fcaebook
  d.findElement(By.id("email")).sendKeys("email");
  d.findElement(By.id("pass")).sendKeys("password");
  d.findElement(By.id("loginbutton")).click();
   Thread.sleep(3000);

    //5. Post "Hello World" into status 

   d.get("https://www.facebook.com/");
   WebElement post= d.findElement(By.xpath("//*[@name='xhpc_message']"));
   post.click();
   post.sendKeys("Hello World");
   d.findElement(By.xpath("(//button[@value='1'])[5]")).click();

   Thread.sleep(2000);

  }