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!')