-
Notifications
You must be signed in to change notification settings - Fork 0
/
facebook.py
73 lines (61 loc) · 2.34 KB
/
facebook.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
""" Facebook Bot """
""" Desarrollado por Juan Carlos Muñoz Guzmán """
"""
Version 1
Funciones
*Login
*Commentar
*Cerrar sesión
"""
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import sys
import time
class FacebookBot():
# Init
def __init__(self, user, password):
#Chrome o Firefox
# need webdriver
self.chromeoptions = webdriver.ChromeOptions()
self.prefs = {
"profile.default_content_setting_values.notifications": 2
}
self.chromeoptions.add_experimental_option("prefs", self.prefs)
self.chromeoptions.add_argument("headless")
self.chromeoptions.add_argument("disable-gpu")
self.chromeoptions.add_argument("no-sandbox")
self.driver = webdriver.Chrome(executable_path='C:/webdrivers/chromedriver.exe', chrome_options=self.chromeoptions)
self.user = user
self.password = password
def login_facebook(self):
dr = self.driver
dr.get('https://www.facebook.com/')
time.sleep(2)
print("Se ha cargado la web")
# declare elements of web(facebook)
elem_user = dr.find_element_by_xpath("//input[@id='email']")
elem_user.clear()
elem_user.send_keys(self.user)
elem_password = dr.find_element_by_xpath("//input[@id='pass']")
elem_password.clear()
elem_password.send_keys(self.password)
elem_password.send_keys(Keys.ENTER)
time.sleep(5)
print("Has iniciado sesión")
def comment_facebook(self, message):
dr = self.driver
elem_comment = dr.find_element_by_xpath("//textarea[@name='xhpc_message']")
elem_comment.click()
elem_comment.send_keys(message)
time.sleep(2)
elem_okay = dr.find_element_by_xpath("//button[@class='_1mf7 _4r1q _4jy0 _4jy3 _4jy1 _51sy selected _42ft']")
elem_okay.click()
print("Post creado con exito")
# Credenciales de facebook
usuario_facebook = "[email protected]"
contrasena_facebook = "1234567890."
# Apartado de comentarios
comentario_facebook = "Este es mi primer comentario mediante un bot con python"
obj_bot_facebook = FacebookBot(usuario_facebook, contrasena_facebook)
obj_bot_facebook.login_facebook()
obj_bot_facebook.comment_facebook(comentario_facebook)