Advertisement
Tryhell

azureporn

Dec 4th, 2019 (edited)
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. import undetected_chromedriver as uc
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.support.ui import WebDriverWait
  4. from selenium.webdriver.support import expected_conditions as EC
  5. from selenium.common.exceptions import TimeoutException, NoSuchElementException
  6. import requests
  7. import time
  8. import traceback
  9. import psutil
  10.  
  11. def wait_and_click(xpath, timeout=80):
  12. try:
  13. element = WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((By.XPATH, xpath)))
  14. if xpath == "//*[@id='portals']/div[1]/div/div/div[2]/div[1]/button":
  15. element.click()
  16. else:
  17. driver.execute_script("arguments[0].click();", element)
  18. except Exception as e:
  19. if xpath != "//*[@id='root']/div/div/div[2]/div/div[3]/div/div[2]/div[2]/button":
  20. print(f"Произошла ошибка при клике на элемент '{xpath}': {e}")
  21. restart_script()
  22. else:
  23. print(f"Не удалось нажать на элемент '{xpath}', пропускаем.")
  24.  
  25. def wait_and_send_request(xpath, url, timeout=120):
  26. try:
  27. response = requests.get(url)
  28. if response.status_code == 200:
  29. element = WebDriverWait(driver, timeout).until(EC.visibility_of_element_located((By.XPATH, xpath)))
  30. element.send_keys(response.text)
  31. except Exception as e:
  32. print(f"Произошла ошибка при отправке запроса на '{url}' в элемент '{xpath}': {e}")
  33. restart_script()
  34.  
  35. def setup_driver():
  36. options = uc.ChromeOptions()
  37. options.add_argument("--user-data-dir=C:/Users/pc/AppData/Local/Google/Chrome/User Data")
  38. options.add_argument("--profile-directory=Default")
  39. options.add_argument("--blink-settings=imagesEnabled=false")
  40. options.add_argument("--start-maximized")
  41. return uc.Chrome(options=options)
  42.  
  43. def kill_chrome_processes():
  44. for process in psutil.process_iter():
  45. try:
  46. if process.name().lower() in ['chrome', 'undetected_chromedriver']:
  47. process.kill()
  48. except psutil.NoSuchProcess:
  49. continue
  50.  
  51. def restart_script():
  52. global driver
  53. try:
  54. driver.quit()
  55. except Exception as e:
  56. print(f"Произошла ошибка при закрытии драйвера: {e}")
  57. kill_chrome_processes()
  58. driver = setup_driver()
  59. main()
  60.  
  61. def main():
  62. try:
  63. driver.get("https://web.telegram.org/a/#6855316188")
  64. wait_and_click("//*[@id='message445']/div[3]/div/div[1]/div/a[50]")
  65.  
  66. while True:
  67. wait_and_click("//*[@id='portals']/div[1]/div/div/div[2]/div[2]/iframe")
  68. driver.switch_to.frame(driver.find_element(By.XPATH, "//*[@id='portals']/div[1]/div/div/div[2]/div[2]/iframe"))
  69.  
  70. wait_and_click("//*[@id='root']/div/div/div[1]/header/div[3]/button[2]")
  71. wait_and_click("//*[@id='root']/div/div/div/div[3]/button")
  72. wait_and_send_request("//*[@id='root']/div/div/div[1]/label/textarea", "http://192.168.11.53/hot/hot.php")
  73. wait_and_click("//*[@id='root']/div/div/div[2]/button")
  74. wait_and_click("//*[@id='root']/div/div/button")
  75. wait_and_click("//*[@id='root']/div/div/div[1]/div/div/div[4]/div[2]")
  76.  
  77. # Измененный блок
  78. try:
  79. wait_and_click("//*[@id='root']/div/div/div[2]/div/div[3]/div/div[2]/div[2]/button", 1)
  80. except Exception as e:
  81. print(f"Произошла ошибка при клике на кнопку: {e}")
  82.  
  83. driver.switch_to.default_content()
  84. wait_and_click("//*[@id='portals']/div[1]/div/div/div[2]/div[1]/button")
  85.  
  86. except Exception as e:
  87. print(f"Произошла ошибка: {e}")
  88. traceback.print_exc()
  89. restart_script()
  90.  
  91. driver = setup_driver()
  92. while True:
  93. try:
  94. main()
  95. except Exception as e:
  96. print(f"Произошла критическая ошибка: {e}")
  97. traceback.print_exc()
  98. restart_script()
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement