im coding for automate many posts for many pages at the same time, but i can´t find a proper way to “type” or “paste” text on the input box.
also i don’t know how to paste images properly, i tried simulating CTRL + V with an image in the clipboard, using send_keys(image) with no success
when i try to paste or put the full text in a single action it just writes the first word of every line
Hi
How
are you?
will just write
H
H
a
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import os
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
print("navegador")
#abre navegador
profile = webdriver.FirefoxProfile()
profile.set_preference("dom.webnotifications.enabled", False)
browser = webdriver.Firefox(options=profile)
browser.get('https://www.facebook.com')
browser.maximize_window()
print("datos")
#carga datos
arch3 = open("datos.txt", "r")
user = []
arch2 = open("grupos.txt", 'r')
list = []
for i in arch2.readlines():
list.append(i)
arch2.close()
for i in arch3.readlines():
user.append(i)
arch3.close()
print("login")
#login
login = browser.find_element(By.ID, "email")
login.send_keys(user[0])
p = browser.find_element(By.ID, "pass")
p.send_keys(user[1])
time.sleep(5)
#p.send_keys(Keys.RETURN)
try:
boton = browser.find_element(By.XPATH, "//*[starts-with (@id, 'u_0_5_')]")
boton.click()
time.sleep(10)
except:
print("No Login")
time.sleep(25000)
print("grupo")
#grupo
for i in list:
grupos = 0
browser.get(i)
print("post")
#Post
time.sleep(20)
post = WebDriverWait(browser, 40).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div/div/div[1]/div/div[3]/div/div/div[1]/div[1]/div[4]/div/div[2]/div/div/div[2]/div[1]/div/div/div/div[1]/div")))
post.click()
print("post 1.1")
time.sleep(20)
post2 = WebDriverWait(browser, 40).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div/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/div[2]/div[1]/div[1]/div[1]/div[1]/div/div/div[1]")))
post2.click()
post2.send_keys("Hello World")
time.sleep(25000)
The unique way that i found for doing it was with a loop and with send_keys() but it’s slower than a human tipically typing so it’s not optimal
There are some things that im not using at this moment and the code can be optimized but i made it like this just for testing.
-
the prints are for following the code execution
-
the send_keys(Keys.RETURN) is for avoiding locating the login button but it just start successfully 1/2 times
-
the last time.sleep() is for checking XPATH/ID/CLASS, etc.
-
Some time.sleeps() are for adjusting to loading times, i’ll change it to WebDriverWait.
1