Skip to content

Instantly share code, notes, and snippets.

@manrajgrover
Created January 30, 2015 12:59
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save manrajgrover/ba1bd439d3d5a59feaaf to your computer and use it in GitHub Desktop.
Save manrajgrover/ba1bd439d3d5a59feaaf to your computer and use it in GitHub Desktop.
Selenium script to log in to Facebook and post on wall
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.common.exceptions import NoSuchElementException
driver = webdriver.Firefox()
driver.get('https://www.facebook.com/')
print "Opened facebook..."
a = driver.find_element_by_id('email')
a.send_keys('Your email id')
print "Email Id entered..."
b = driver.find_element_by_id('pass')
b.send_keys('Your password')
print "Password entered..."
c = driver.find_element_by_id('loginbutton')
c.click()
driver.get("https://www.facebook.com/apoorvu")
post_box=driver.find_element_by_xpath("//*[@name='xhpc_message']")
post_box.click()
post_box.send_keys("Testing using Name not ID.Selenium is easy.")
sleep(2)
post_it=driver.find_element_by_xpath("//*[@id='u_0_1a']/div/div[6]/div/ul/li[2]/button")
post_it.click()
print "Posted..."
@jebastin4u
Copy link

i got this error

Opened facebook...
Email Id entered...
Password entered...
Traceback (most recent call last):
File "new_login.py", line 23, in
post_box=driver.find_element_by_xpath("//[@name='xhpc_message']")
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 293, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 752, in find_element
'value': value})['value']
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //
[@name='xhpc_message']

@PhanNN
Copy link

PhanNN commented Jun 26, 2017

@jebastin4u: Using 'xhpc_message_text' instead of 'xhpc_message'

@lucasweiss
Copy link

lucasweiss commented Jul 18, 2017

hello,

Any idea about adding emoji to the message ?

str_mess = 'hello \emoji here//'
...
post_box.send_keys(str_mess)

it is not working on my side...

Any help please !
Regards

@JoaoGFarias
Copy link

What is the rationale for .xhpc_message? I cannot find this element on the Chrome/Firefox Inspector, but it unexpectedly works.

@wupeiwen123
Copy link

add to driver.refresh()
@jebastin4u

@prachi1111
Copy link

Hello , Any idea about sharing a post ?

@TahirJalilov
Copy link

Hello, how to upload image to my post?

@anassNasserallah
Copy link

I got an error when I use //[@name='xhpc_message'] or //[@name='xhpc_message_text']

help please

@shujinkouu
Copy link

Hi there, so I stumbled upon this article, obviously the trick doesn't work as is in 2020 I do believe most of the field changed names in the meantime I recoded a version of the same thing.
So I used the knowledge from this post and I added stuff from those sources:
https://www.google.com/search?q=driver.find_element_by_xpath&oq=driver.find_element_by_xpath&aqs=chrome..69i57&sourceid=chrome&ie=UTF-8
https://stackoverflow.com/questions/19035186/how-to-select-element-using-xpath-syntax-on-selenium-for-python
https://www.youtube.com/watch?v=XU-pQc4T-U0
https://www.youtube.com/watch?v=kpxfcaIh9ZY

and here is my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.common.exceptions import NoSuchElementException

def init():
driver = webdriver.Firefox()
driver.get('https://www.facebook.com/')
return driver

def credentials():
email = input('what is your email or phone number?\n')
password = input('what is your password?\n')
return email,password

def getcontrol(driver):
a = driver.find_element_by_xpath(".//[@id='email']")
b = driver.find_element_by_xpath(".//
[@id='pass']")
c = driver.find_element_by_name("login")
return a,b,c

def injectcredentials(a,b,c,email,password):
a.send_keys(email)
b.send_keys(password)
c.click()

def posting(driver,msg):
sleep(2)
comment = driver.find_element_by_xpath("//[@Class='m9osqain a5q79mjw jm1wdb64 k4urcfbm']")
comment.click()
sleep(4)
post_box=driver.switch_to.active_element
post_box.send_keys(msg)
sleep(2)
publish = driver.find_element_by_xpath("//
[@Class='oajrlxb2 s1i5eluu gcieejh5 bn081pho humdl8nn izx4hr6d rq0escxv nhd2j8a9 j83agx80 p7hjln8o kvgmc6g5 cxmmr5t8 oygrvhab hcukyx3x jb3vyjys d1544ag0 qt6c0cv9 tw6a2znq i1ao9s8h esuyzwwr f1sip0of lzcic4wl l9j0dhe7 abiwlrkh p8dawk7l beltcj47 p86d2i9g aot14ch1 kzx2olss cbu4d94t taijpn5t ni8dbmo4 stjgntxs k4urcfbm tv7at329']")
publish.click()
sleep(5)

[email,password] = credentials()
msg = input('what do you want to post today?\n')

driver = init()

[a,b,c] = getcontrol(driver)
injectcredentials(a,b,c,email,password)

posting(driver,msg)

#driver.quit()

it is possible most line that look like: comment = driver.find_element_by_xpath("//*[@Class='m9osqain a5q79mjw jm1wdb64 k4urcfbm']")
will not work for you, see this oddly specific class name well I believe this thing is almost random and changes from time to time the holy grail would be to have many ways of selecting the darn thing and use try: for all of them to handle any potential errors. Hope that helped anyone and let me know if you guys find something cool I could use. Much love <3 Enjoy coding :)

@danyarango1999
Copy link

I have the same error with Can't locate item: {"method": "xpath", "selector": "// * [@ name = 'xhpc_message_text']"}
is to create a publication in the group someone I can solve who can help me
Unable to locate element: {"method":"xpath","selector":"//*[@name='xhpc_message_text']"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment