diff --git a/schwab_scraper/features/accounts_positions/positions_scraper.py b/schwab_scraper/features/accounts_positions/positions_scraper.py index a6a655c..2e704ec 100644 --- a/schwab_scraper/features/accounts_positions/positions_scraper.py +++ b/schwab_scraper/features/accounts_positions/positions_scraper.py @@ -439,29 +439,6 @@ async def get_positions( await page.wait_for_selector('#positionsDetails', timeout=45000) await page.wait_for_timeout(1000) - # Try to expand lots using a more reliable evaluate-based approach - try: - expanded_count = await page.evaluate(""" - () => { - const buttons = Array.from(document.querySelectorAll('tr.position-row sdps-button[sdps-id="costBasisTBD"] button')); - let count = 0; - buttons.forEach(btn => { - // Check if already expanded (usually has a different icon or state, but clicking again often toggles) - // For now we just click them all. - btn.click(); - count++; - }); - return count; - } - """) - if debug: - print(f"Clicked {expanded_count} potential lot expanders") - if expanded_count > 0: - await page.wait_for_timeout(2000) # Wait for expansion - except Exception as e: - if debug: - print(f"Error expanding lots: {e}") - await page.evaluate('window.scrollTo(0, document.body.scrollHeight)') await page.wait_for_timeout(1500) @@ -551,18 +528,20 @@ async def get_positions( # Wait for modal to appear await page.wait_for_timeout(1000) - # Find the active modal (not inert, visible) + # Find the active modal (not inert, visible, and matches our symbol) modal_handle = None modals = await page.query_selector_all('app-lot sdps-modal[sdps-id="open-lot-overlay"]') for m in modals: is_hidden = await m.evaluate('el => el.getAttribute("aria-hidden") === "true" || el.hasAttribute("inert")') - if not is_hidden: + if is_hidden: + continue + + # Verify title matches symbol to avoid stale modal data + title_text = await m.evaluate('el => el.querySelector(".sdps-modal__title")?.innerText || ""') + if symbol.upper() in title_text.upper(): modal_handle = m break - if not modal_handle and modals: - modal_handle = modals[-1] # Fallback to last one - if modal_handle: modal_id = await modal_handle.get_attribute('modal-id') if debug: @@ -596,7 +575,14 @@ async def get_positions( close_btn = await modal_handle.query_selector('button.sdps-modal__close') if close_btn: await close_btn.click(force=True) - await page.wait_for_timeout(1000) + # Wait for modal to actually be removed or hidden + try: + await page.wait_for_selector(f'app-lot sdps-modal[modal-id="{modal_id}"]', state='hidden', timeout=3000) + except Exception: + pass + else: + if debug: + print(f"DEBUG: Could not find matching visible modal for {symbol}") except Exception as e: if debug: print(f"Error expanding lots for {symbol}: {e}")