From c11f0586dda902f5cd279d9340e63a703e7f180c Mon Sep 17 00:00:00 2001 From: Stephen Kent Date: Sun, 9 Jul 2023 22:47:43 -0700 Subject: [PATCH] Skip cookie prompt if not present before starting sign in flow --- safeway_coupons/session.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/safeway_coupons/session.py b/safeway_coupons/session.py index da51555..f77e2c8 100644 --- a/safeway_coupons/session.py +++ b/safeway_coupons/session.py @@ -7,7 +7,7 @@ import requests import selenium.webdriver.support.expected_conditions as ec import undetected_chromedriver as uc # type: ignore -from selenium.common.exceptions import TimeoutException +from selenium.common.exceptions import NoSuchElementException, TimeoutException from selenium.webdriver.remote.webdriver import By from selenium.webdriver.support.wait import WebDriverWait @@ -87,12 +87,16 @@ def _login(self, account: Account) -> None: url = "https://www.safeway.com" print("Connect to safeway.com") driver.get(url) - button = driver.find_element( - By.XPATH, "//button [contains(text(), 'Necessary Only')]" - ) - if button: - print("Decline cookie prompt") - button.click() + try: + button = driver.find_element( + By.XPATH, + "//button [contains(text(), 'Necessary Only')]", + ) + if button: + print("Decline cookie prompt") + button.click() + except NoSuchElementException: + print("Skipping cookie prompt which is not present") print("Open Sign In sidebar") wait.until( ec.visibility_of_element_located(