Answer the question
In order to leave comments, you need to log in
The message is sent to the site, but other users do not see it, why?
I wrote a chat bot for a friend in tik tok, on a bunch of python + selenium. The problem is that it reacts normally, sends a response to messages to the chat, but no one sees the bot messages. I authorize through VK, I throw the link immediately to the broadcast
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import undetected_chromedriver.v2 as uc
import data
url = input('Введите адрес: ')
options = uc.ChromeOptions()
#options.add_argument('--headless')
user_agent = 'Naverbot'
options.add_argument(f'user-agent={user_agent}')
options.add_argument("--window-size=1920,1080")
options.add_argument("--disable-extensions")
options.add_argument("--proxy-server='direct://'")
options.add_argument("--proxy-bypass-list=*")
options.add_argument("--start-maximized")
options.add_argument('--disable-gpu')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--no-sandbox')
options.add_argument('--ignore-certificate-errors')
#driver = webdriver.Edge(options=options)
driver = uc.Chrome()
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
const newProto = navigator.__proto__
delete newProto.webdriver
navigator.__proto__ = newProto
"""
})
driver.set_window_size(1920,1080)
try:
driver.get(url)
sleep(1)
try:
iframe = driver.find_element(By.XPATH, '//iframe[@class="tiktok-tpndsz-IframeLoginSite eaxh4gs3"]')
sleep(3)
except:
button_to_log = driver.find_elements(By.TAG_NAME, 'button')[1].click()
sleep(3)
iframe = driver.find_element(By.XPATH,'//iframe[@class="tiktok-tpndsz-IframeLoginSite eaxh4gs3"]')
sleep(1)
sleep(3)
driver.switch_to.frame(iframe)
vk_login = driver.find_elements(By.XPATH,'//div[@class="channel-name-2qzLW"]')[2].click()
driver.switch_to.window(driver.window_handles[1])
email = driver.find_element(By.NAME,'email')
email.clear()
email.send_keys(data.mail)
sleep(3)
password = driver.find_element(By.NAME,'pass')#Почта, берется и файла Data.py
password.clear()
password.send_keys(data.password,Keys.ENTER)#Пароль, берется и файла Data.py
sleep(3)
print('Авторизация завершена')
driver.switch_to.window(driver.window_handles[0])
sleep(15)
file = open('text.txt','r',encoding='utf-8')
dictionary = file.readlines()
dictionary = [x.split('--') for x in dictionary]
file.close()
for i in dictionary:
i[1] = i[1].replace('\n', '')
dictionary = dict(dictionary)
buffer = []
#в строках 74-79 я достаю текст, чтобы он стал словарем формата сообщение:ответ
while True:
try:
text = driver.find_element(By.XPATH,'(//div[contains(@class,"DivChatMessageContent")])[last()]//span[contains(@class,"SpanChatRoomComment") ]')
#Смотрю последнее сообщение
if text not in buffer:
buffer = []
buffer.append(text)
try:
message = dictionary[text.text]
print(text.text)
sleep(0.1)
pseudo_input = driver.find_element(By.CSS_SELECTOR, ".tiktok-7svj0w-DivCommentContainer")
(ActionChains(driver)
.move_to_element(pseudo_input)
.click(pseudo_input)
.pause(0.1)
.send_keys_to_element(pseudo_input,' ' + message)
.pause(2)
.send_keys_to_element(pseudo_input, Keys.ENTER)
.perform()
)
except KeyError:
continue
except:
continue
finally:
driver.close()
driver.quit()
print('DONE!')
Answer the question
In order to leave comments, you need to log in
In general, I understood what the problem was, it’s just that instead of the enter button, you need to emulate the send message button, otherwise the site will not see this message:
pseudo_input = driver.find_element(By.CSS_SELECTOR, ".tiktok-7svj0w-DivCommentContainer")
(ActionChains(driver)
.move_to_element(pseudo_input)
.click(pseudo_input)
.pause(0.1)
.send_keys_to_element(pseudo_input,' ' + message)
.pause(0.4)
.perform()
)
enter = driver.find_element(By.CSS_SELECTOR,'.tiktok-1vgtakc-DivPostButton').click()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question