Skip to content

Commit c31b974

Browse files
authored
Merge pull request #98 from MaineDSA/handle_modal
close modal popup if present
2 parents f40aa57 + c99e3a1 commit c31b974

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/automation.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
MIN_SCROLL_UP,
2020
MIN_WAIT_TIME,
2121
PROBABILITY_SCROLL_UP,
22+
ZillowParseError,
2223
)
2324
from src.scraper import PropertyListing, ZillowHomeFinder
2425

@@ -64,6 +65,20 @@ async def get_browser_page(context: BrowserContext, *, require_new_page: bool =
6465
# Browser Automation - Scrolling and Navigation
6566

6667

68+
async def close_modal_if_present(page: Page) -> None:
69+
"""Close modal dialog by clicking button with class containing 'CloseButton', if present."""
70+
try:
71+
close_button = page.locator("button[class*='CloseButton']").first
72+
is_visible = await close_button.is_visible()
73+
if not is_visible:
74+
msg = "Popup modal blocked page loading, cannot scrape."
75+
raise ZillowParseError(msg)
76+
logger.debug("Popup modal detected, closing it")
77+
await close_button.click()
78+
except TimeoutError as e:
79+
logger.debug("No CloseButton modal found or could not close: %s", e)
80+
81+
6782
async def get_property_card_count(page: Page) -> int:
6883
"""Get the current count of loaded property cards."""
6984
cards = await page.query_selector_all('article[data-test="property-card"]')

src/main.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@
55

66
from patchright.async_api import BrowserContext
77

8-
from src.automation import create_browser_context, deduplicate_listings, get_browser_page, scrape_all_pages, simulate_human_behavior, sort_by_newest
8+
from src.automation import (
9+
close_modal_if_present,
10+
create_browser_context,
11+
deduplicate_listings,
12+
get_browser_page,
13+
scrape_all_pages,
14+
simulate_human_behavior,
15+
sort_by_newest,
16+
)
917
from src.config import Config, SubmissionType, load_configs
1018
from src.form_submission import submit_listings
1119
from src.scraper import PropertyListing
@@ -26,6 +34,8 @@ async def scrape_listings(context: BrowserContext, config: Config) -> list[Prope
2634
error_msg = "CAPTCHA detected, cannot continue."
2735
raise BaseException(error_msg)
2836

37+
await close_modal_if_present(page)
38+
2939
logger.info("Scraping all listings...")
3040
await sort_by_newest(page)
3141
all_listings = await scrape_all_pages(page)

0 commit comments

Comments
 (0)