Skip to content

'Locator' object has no attribute '_page' #154

@Zach10za

Description

@Zach10za

Full stacktrace:

Error performing act from ObserveResult: 'Locator' object has no attribute '_page', exception='Locator' object has no attribute '_page' stack_trace=Traceback (most recent call last):
  File "/opt/venv/lib/python3.11/site-packages/stagehand/handlers/act_handler.py", line 193, in _act_from_observe_result
    await self._perform_playwright_method(
  File "/opt/venv/lib/python3.11/site-packages/stagehand/handlers/act_handler.py", line 321, in _perform_playwright_method
    raise e
  File "/opt/venv/lib/python3.11/site-packages/stagehand/handlers/act_handler.py", line 305, in _perform_playwright_method
    await method_fn(context)
  File "/opt/venv/lib/python3.11/site-packages/stagehand/handlers/act_handler_utils.py", line 339, in press_key
    raise e
  File "/opt/venv/lib/python3.11/site-packages/stagehand/handlers/act_handler_utils.py", line 310, in press_key
    await ctx.locator._page.keyboard.press(key)
          ^^^^^^^^^^^^^^^^^
AttributeError: 'Locator' object has no attribute '_page'

Steps to reproduce

stagehand = Stagehand(config)

await stagehand.init()

page = stagehand.page

await page.goto('https://www.instacart.com/', wait_until='domcontentloaded')

observation = await page.observe("Click on the search bar")
await page.act(observation[0])

observation = await page.observe("Type 'bananas'")
await page.act(observation[0])

observation = await page.observe("Hit Enter")
await page.act(observation[0])

This can also be seen in the test_keyboard_actions_local test. Not sure why it doesn't cause the test to fail though.

Solution

I believe the issue is here:
https://github.com/browserbase/stagehand-python/blob/main/stagehand/handlers/act_handler_utils.py#L310C9-L310C52

There are two possible solutions as far as I can tell:

  1. Press the enter key from the page.
- await ctx.locator._page.keyboard.press(key)
+ await ctx.stagehand_page._page.keyboard.press(key)
  1. Press the enter key from the locator. (Preferred by Playwright)
- await ctx.locator._page.keyboard.press(key)
+ await ctx.locator.press(key)

Other potential errors

I see ctx.locator._page also called in the first log in fallback_locator_method in the same file. I assume this fails as well but I haven't confirmed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions