Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observe perform candidates #426

Merged
merged 26 commits into from
Jan 29, 2025
Merged

Observe perform candidates #426

merged 26 commits into from
Jan 29, 2025

Conversation

miguelg719
Copy link
Collaborator

@miguelg719 miguelg719 commented Jan 23, 2025

why

Observe got a major upgrade. Now it will return a suggested playwright method with any necessary arguments for the generated candidate elements. It also includes a significant speedup when using a11y tree processing for context.

what changed

The output of observe calls used to be in the format:

[
  {
    description: 'A list element that...',
    selector: 'xpath=/html/body/div[1]/main/section/div[1]/section[2]/div[2]/ul'
  }
]

Now, observe returns the following:

[
  {
    description: 'The scrollable sidebar containing ...',
    method: 'scrollIntoViewIfNeeded', // or another playwright method such as click, type, etc...
    arguments: [],
    selector: 'xpath=/html/body/div[1]/main/section/div[1]/section[2]/div[2]/ul'
  }
]

On top of that, the a11y tree context parsing / candidate generation got a major speedup by skipping processAllDom.

With a11y trees, the context might include elements present in the DOM but behind some effect in the UI. To give users control on whether to include these in the context or not, we provide the flag onlyVisible. In essence, setting it to true will default to the previous observe method, and false (default) will use the new features described in this PR.

Sample usage:

const observed = await stagehand.page.observe({
  instruction:
    "find the scrollable sidebar element that contains all the real estate listings",
    onlyVisible: true,    // defaults to false
    returnAction: true,   // defaults to false
});

test plan

  • Create new evals
  • Test on new evals

Copy link

changeset-bot bot commented Jan 23, 2025

🦋 Changeset detected

Latest commit: 9776827

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@browserbasehq/stagehand Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@miguelg719
Copy link
Collaborator Author

need to check if _waitForSettledDom is completely necessary in public performPlaywrightMethod

@miguelg719 miguelg719 marked this pull request as ready for review January 28, 2025 02:31
@miguelg719
Copy link
Collaborator Author

just missing adding changeset

@miguelg719 miguelg719 added the observe These changes pertain to the observe function label Jan 28, 2025
lib/StagehandPage.ts Show resolved Hide resolved
lib/StagehandPage.ts Show resolved Hide resolved
lib/a11y/utils.ts Show resolved Hide resolved
lib/a11y/utils.ts Outdated Show resolved Hide resolved
await newOpenedTab.close();
await stagehandPage.goto(newOpenedTab.url());
await stagehandPage.waitForLoadState("domcontentloaded");
// await stagehandPage._waitForSettledDom(domSettleTimeoutMs);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: figure out how to get around this -- maybe we make waitForSettledDom a function that takes in a page?

@@ -76,65 +78,22 @@ export class StagehandObserveHandler {
},
});

let outputString: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking my understanding: we're removing this because we're not cross-referencing DOM with a11y tree, and now we're basically just removing a11y tree if the user insists on only observing visible elements

lib/handlers/observeHandler.ts Show resolved Hide resolved
lib/handlers/observeHandler.ts Outdated Show resolved Hide resolved
lib/handlers/observeHandler.ts Outdated Show resolved Hide resolved
lib/handlers/observeHandler.ts Outdated Show resolved Hide resolved
@kamath kamath self-requested a review January 29, 2025 18:42
@miguelg719 miguelg719 merged commit bbbcee7 into main Jan 29, 2025
10 checks passed
@github-actions github-actions bot mentioned this pull request Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
observe These changes pertain to the observe function
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants