webdriver.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. from __future__ import annotations
  2. from platformdirs import user_config_dir
  3. from selenium.webdriver.remote.webdriver import WebDriver
  4. from undetected_chromedriver import Chrome, ChromeOptions
  5. from selenium.webdriver.common.by import By
  6. from selenium.webdriver.support.ui import WebDriverWait
  7. from selenium.webdriver.support import expected_conditions as EC
  8. from os import path
  9. from . import debug
  10. try:
  11. from pyvirtualdisplay import Display
  12. has_pyvirtualdisplay = True
  13. except ImportError:
  14. has_pyvirtualdisplay = False
  15. def get_browser(
  16. user_data_dir: str = None,
  17. headless: bool = False,
  18. proxy: str = None,
  19. options: ChromeOptions = None
  20. ) -> WebDriver:
  21. if user_data_dir == None:
  22. user_data_dir = user_config_dir("g4f")
  23. if user_data_dir and debug.logging:
  24. print("Open browser with config dir:", user_data_dir)
  25. if not options:
  26. options = ChromeOptions()
  27. if proxy:
  28. options.add_argument(f'--proxy-server={proxy}')
  29. driver = '/usr/bin/chromedriver'
  30. if not path.isfile(driver):
  31. driver = None
  32. return Chrome(
  33. options=options,
  34. user_data_dir=user_data_dir,
  35. driver_executable_path=driver,
  36. headless=headless
  37. )
  38. def bypass_cloudflare(driver: WebDriver, url: str, timeout: int) -> None:
  39. # Open website
  40. driver.get(url)
  41. # Is cloudflare protection
  42. if driver.find_element(By.TAG_NAME, "body").get_attribute("class") == "no-js":
  43. if debug.logging:
  44. print("Cloudflare protection detected:", url)
  45. try:
  46. # Click button in iframe
  47. WebDriverWait(driver, 5).until(
  48. EC.presence_of_element_located((By.CSS_SELECTOR, "#turnstile-wrapper iframe"))
  49. )
  50. driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, "#turnstile-wrapper iframe"))
  51. WebDriverWait(driver, 5).until(
  52. EC.presence_of_element_located((By.CSS_SELECTOR, "#challenge-stage input"))
  53. )
  54. driver.find_element(By.CSS_SELECTOR, "#challenge-stage input").click()
  55. except:
  56. pass
  57. finally:
  58. driver.switch_to.default_content()
  59. # No cloudflare protection
  60. WebDriverWait(driver, timeout).until(
  61. EC.presence_of_element_located((By.CSS_SELECTOR, "body:not(.no-js)"))
  62. )
  63. class WebDriverSession():
  64. def __init__(
  65. self,
  66. webdriver: WebDriver = None,
  67. user_data_dir: str = None,
  68. headless: bool = False,
  69. virtual_display: bool = False,
  70. proxy: str = None,
  71. options: ChromeOptions = None
  72. ):
  73. self.webdriver = webdriver
  74. self.user_data_dir = user_data_dir
  75. self.headless = headless
  76. self.virtual_display = None
  77. if has_pyvirtualdisplay and virtual_display:
  78. self.virtual_display = Display(size=(1920, 1080))
  79. self.proxy = proxy
  80. self.options = options
  81. self.default_driver = None
  82. def reopen(
  83. self,
  84. user_data_dir: str = None,
  85. headless: bool = False,
  86. virtual_display: bool = False
  87. ) -> WebDriver:
  88. if user_data_dir == None:
  89. user_data_dir = self.user_data_dir
  90. if self.default_driver:
  91. self.default_driver.quit()
  92. if not virtual_display and self.virtual_display:
  93. self.virtual_display.stop()
  94. self.virtual_display = None
  95. self.default_driver = get_browser(user_data_dir, headless, self.proxy)
  96. return self.default_driver
  97. def __enter__(self) -> WebDriver:
  98. if self.webdriver:
  99. return self.webdriver
  100. if self.virtual_display:
  101. self.virtual_display.start()
  102. self.default_driver = get_browser(self.user_data_dir, self.headless, self.proxy, self.options)
  103. return self.default_driver
  104. def __exit__(self, exc_type, exc_val, exc_tb):
  105. if self.default_driver:
  106. try:
  107. self.default_driver.close()
  108. except:
  109. pass
  110. self.default_driver.quit()
  111. if self.virtual_display:
  112. self.virtual_display.stop()