From 991e4e939a8db426d931223729b0d9fff86911a9 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sat, 25 Nov 2023 16:03:42 +0000 Subject: [PATCH] Dismiss modals --- google_stocks/__init__.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/google_stocks/__init__.py b/google_stocks/__init__.py index 31b4cba..dbdb0fa 100644 --- a/google_stocks/__init__.py +++ b/google_stocks/__init__.py @@ -7,7 +7,7 @@ import urllib.parse from datetime import datetime import lxml.html -from playwright.sync_api import Playwright, expect, sync_playwright +from playwright.sync_api import Page, Playwright, expect, sync_playwright auth_file = os.path.expanduser("~/lib/auth/google.json") data_loc = os.path.expanduser("~/lib/google_stocks") @@ -34,6 +34,25 @@ def data_filename(page_type: str, ext: str = "html") -> str: return os.path.join(data_loc, now_str + f"_{page_type}.{ext}") +def accept_cookies(page: Page) -> None: + """Check for the 'Accept all' button and click it if found.""" + accept_button_selector = "button:has-text('Accept all')" + accept_button = page.locator(accept_button_selector) + if accept_button.is_visible(): + accept_button.click() + + +def stay_signed_out(page: Page) -> None: + """Check for the 'Stay signed out' button and click it if found.""" + # Selector for the 'Stay signed out' button + button_selector = "text=Stay signed out" + + # Check for the button and click it if found + stay_signed_out_button = page.locator(button_selector) + if stay_signed_out_button.is_visible(): + stay_signed_out_button.click() + + class Index: """Stock market index.""" @@ -71,6 +90,10 @@ class Index: page = context.new_page() page.goto(self.search_url, wait_until="domcontentloaded") + + accept_cookies(page) + stay_signed_out(page) + expect(page.get_by_text("Market Summary")).to_be_visible() html = page.content()