From 7a81ab617a665a7768e6d9984a16b9ee8b77baa2 Mon Sep 17 00:00:00 2001 From: Patrick Robertson Date: Tue, 11 Mar 2025 11:57:25 +0000 Subject: [PATCH] Better checking of cookies to add to webdriver --- src/auto_archiver/utils/webdriver.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/auto_archiver/utils/webdriver.py b/src/auto_archiver/utils/webdriver.py index cb4e2a9..af3b7dd 100644 --- a/src/auto_archiver/utils/webdriver.py +++ b/src/auto_archiver/utils/webdriver.py @@ -3,6 +3,7 @@ from __future__ import annotations import os import time +import re #import domain_for_url from urllib.parse import urlparse, urlunparse @@ -48,8 +49,9 @@ class CookieSettingDriver(webdriver.Firefox): self.driver.add_cookie({'name': name, 'value': value}) elif self.cookiejar: domain = urlparse(url).netloc.lstrip("www.") + regex = re.compile(f"(www)?\.?{domain}$") for cookie in self.cookiejar: - if domain in cookie.domain: + if regex.match(cookie.domain): try: self.add_cookie({ 'name': cookie.name, @@ -60,7 +62,7 @@ class CookieSettingDriver(webdriver.Firefox): 'expiry': cookie.expires }) except Exception as e: - logger.warning(f"Failed to add cookie to webdriver: {e}") + logger.warning(f"Failed to add cookie ({cookie.domain}) to webdriver for url {domain}: {e}") if self.facebook_accept_cookies: try: @@ -81,7 +83,7 @@ class CookieSettingDriver(webdriver.Firefox): # try and click the 'close' button on the 'login' window to close it try: xpath = "//div[@role='dialog']//div[@aria-label='Close']" - WebDriverWait(self, 5).until(EC.element_to_be_clickable((By.XPATH, xpath))).click() + WebDriverWait(self, 2).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