Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import undetected_chromedriver as uc
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
- from selenium.common.exceptions import TimeoutException, NoSuchElementException
- import requests
- import time
- import traceback
- import psutil
- def wait_and_click(xpath, timeout=80):
- try:
- element = WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((By.XPATH, xpath)))
- if xpath == "//*[@id='portals']/div[1]/div/div/div[2]/div[1]/button":
- element.click()
- else:
- driver.execute_script("arguments[0].click();", element)
- except Exception as e:
- if xpath != "//*[@id='root']/div/div/div[2]/div/div[3]/div/div[2]/div[2]/button":
- print(f"Произошла ошибка при клике на элемент '{xpath}': {e}")
- restart_script()
- else:
- print(f"Не удалось нажать на элемент '{xpath}', пропускаем.")
- def wait_and_send_request(xpath, url, timeout=120):
- try:
- response = requests.get(url)
- if response.status_code == 200:
- element = WebDriverWait(driver, timeout).until(EC.visibility_of_element_located((By.XPATH, xpath)))
- element.send_keys(response.text)
- except Exception as e:
- print(f"Произошла ошибка при отправке запроса на '{url}' в элемент '{xpath}': {e}")
- restart_script()
- def setup_driver():
- options = uc.ChromeOptions()
- options.add_argument("--user-data-dir=C:/Users/pc/AppData/Local/Google/Chrome/User Data")
- options.add_argument("--profile-directory=Default")
- options.add_argument("--blink-settings=imagesEnabled=false")
- options.add_argument("--start-maximized")
- return uc.Chrome(options=options)
- def kill_chrome_processes():
- for process in psutil.process_iter():
- try:
- if process.name().lower() in ['chrome', 'undetected_chromedriver']:
- process.kill()
- except psutil.NoSuchProcess:
- continue
- def restart_script():
- global driver
- try:
- driver.quit()
- except Exception as e:
- print(f"Произошла ошибка при закрытии драйвера: {e}")
- kill_chrome_processes()
- driver = setup_driver()
- main()
- def main():
- try:
- driver.get("https://web.telegram.org/a/#6855316188")
- wait_and_click("//*[@id='message445']/div[3]/div/div[1]/div/a[50]")
- while True:
- wait_and_click("//*[@id='portals']/div[1]/div/div/div[2]/div[2]/iframe")
- driver.switch_to.frame(driver.find_element(By.XPATH, "//*[@id='portals']/div[1]/div/div/div[2]/div[2]/iframe"))
- wait_and_click("//*[@id='root']/div/div/div[1]/header/div[3]/button[2]")
- wait_and_click("//*[@id='root']/div/div/div/div[3]/button")
- wait_and_send_request("//*[@id='root']/div/div/div[1]/label/textarea", "http://192.168.11.53/hot/hot.php")
- wait_and_click("//*[@id='root']/div/div/div[2]/button")
- wait_and_click("//*[@id='root']/div/div/button")
- wait_and_click("//*[@id='root']/div/div/div[1]/div/div/div[4]/div[2]")
- # Измененный блок
- try:
- wait_and_click("//*[@id='root']/div/div/div[2]/div/div[3]/div/div[2]/div[2]/button", 1)
- except Exception as e:
- print(f"Произошла ошибка при клике на кнопку: {e}")
- driver.switch_to.default_content()
- wait_and_click("//*[@id='portals']/div[1]/div/div/div[2]/div[1]/button")
- except Exception as e:
- print(f"Произошла ошибка: {e}")
- traceback.print_exc()
- restart_script()
- driver = setup_driver()
- while True:
- try:
- main()
- except Exception as e:
- print(f"Произошла критическая ошибка: {e}")
- traceback.print_exc()
- restart_script()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement