Use WebDriverWait when waiting for elements in screenshot enricher

pull/233/head
Patrick Robertson 2025-03-07 12:07:54 +00:00
rodzic e756f1504f
commit dba44b1ac1
1 zmienionych plików z 15 dodań i 11 usunięć

Wyświetl plik

@ -9,8 +9,9 @@ from urllib.parse import urlparse, urlunparse
from http.cookiejar import MozillaCookieJar from http.cookiejar import MozillaCookieJar
from selenium import webdriver from selenium import webdriver
from selenium.common.exceptions import TimeoutException from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.proxy import Proxy, ProxyType from selenium.webdriver.support import expected_conditions as EC
from selenium.common import exceptions as selenium_exceptions
from selenium.webdriver.common.print_page_options import PrintOptions from selenium.webdriver.common.print_page_options import PrintOptions
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By
@ -78,19 +79,22 @@ class CookieSettingDriver(webdriver.Firefox):
super(CookieSettingDriver, self).get(url) super(CookieSettingDriver, self).get(url)
if self.facebook_accept_cookies: if self.facebook_accept_cookies:
# try and click the 'close' button on the 'login' window to close it # try and click the 'close' button on the 'login' window to close it
close_button = self.find_element(By.XPATH, "//div[@role='dialog']//div[@aria-label='Close']") try:
if close_button: xpath = "//div[@role='dialog']//div[@aria-label='Close']"
close_button.click() WebDriverWait(self, 5).until(EC.element_to_be_clickable((By.XPATH, xpath))).click()
except selenium_exceptions.NoSuchElementException:
logger.warning("Unable to find the 'close' button on the facebook login window")
pass
else: else:
# for all other sites, try and use some common button text to reject/accept cookies # for all other sites, try and use some common button text to reject/accept cookies
for text in ["Refuse non-essential cookies", "Decline optional cookies", "Reject additional cookies", "Accept all cookies"]: for text in ["Refuse non-essential cookies", "Decline optional cookies", "Reject additional cookies", "Accept all cookies"]:
try: try:
accept_button = self.find_element(By.XPATH, f"//*[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '{text.lower()}')]") xpath = f"//*[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '{text.lower()}')]"
if accept_button: WebDriverWait(self, 5).until(EC.element_to_be_clickable((By.XPATH, xpath))).click()
accept_button.click() break
break except selenium_exceptions.WebDriverException:
except Exception as e:
pass pass
@ -124,7 +128,7 @@ class Webdriver:
self.driver.set_window_size(self.width, self.height) self.driver.set_window_size(self.width, self.height)
self.driver.set_page_load_timeout(self.timeout_seconds) self.driver.set_page_load_timeout(self.timeout_seconds)
self.driver.print_options = self.print_options self.driver.print_options = self.print_options
except TimeoutException as e: except selenium_exceptions.TimeoutException as e:
logger.error(f"failed to get new webdriver, possibly due to insufficient system resources or timeout settings: {e}") logger.error(f"failed to get new webdriver, possibly due to insufficient system resources or timeout settings: {e}")
return self.driver return self.driver