-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfbbot.py
82 lines (65 loc) · 2.15 KB
/
fbbot.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException
# login details
myEmail = '[email protected]'
myPass = 'myFBpassword'
# search for friend string, must not contain the whole name
myFriend = 'Sveta Goldstein'.decode("utf-8")
myMsg = 'hello world'
# start driver
driver = webdriver.Chrome()
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
# chrome_options.add_argument("--start-maximized")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://www.facebook.com')
# wait for link to load
def loading(link):
print 'loading page...'
while driver.current_url != link:
time.sleep(0.1)
# wait for element to load && to be visible
def getEl(cssSelector):
getEl = True
print "looking for a button"
while getEl:
try:
el = driver.find_element_by_css_selector(cssSelector)
print "got it!"
while not el.is_displayed():
pass
break
except NoSuchElementException, e:
getEl = True
return el
# write message into non-input element
actions = ActionChains(driver)
def writeMsg2el(msg, cssSelector):
actions.move_to_element(driver.find_element_by_css_selector(cssSelector)).send_keys(msg).key_down(Keys.RETURN).key_up(Keys.RETURN).perform()
# initial login
getEl('#login_form [type="email"]').send_keys(myEmail)
getEl('#login_form [type="password"]').send_keys(myPass)
getEl('.uiButtonConfirm').click()
loginLink = 'https://www.facebook.com/'
# loading(loginLink)
print 'login comlete'
# notification popup
# getEl('.uiLayer .uiOverlayFooter a.layerCancel').click()
# activate chat
print 'openeing the chat'
getEl('#fbDockChatBuddylistNub').click()
# search and choose friend to message
print 'looking for a friend'
getEl('#BuddylistPagelet input').send_keys(myFriend)
getEl('._29hk:first-child').click()
print 'found!'
# write friend
getEl('._1mf').click()
writeMsg2el(myMsg, '._1mf')
print 'message is written'