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

[Feature] elementHandle.$ query in whole page instead of the given element #5083

Closed
sarut0bi opened this issue Jan 21, 2021 · 3 comments · Fixed by #5252
Closed

[Feature] elementHandle.$ query in whole page instead of the given element #5083

sarut0bi opened this issue Jan 21, 2021 · 3 comments · Fixed by #5252
Assignees

Comments

@sarut0bi
Copy link

According https://playwright.dev/docs/api/class-elementhandle#elementhandleselector, i should be able to subquery for a given element .

It appears to query the whole page instead of the given element.

Ex:
https://the-internet.herokuapp.com/add_remove_elements/

  • Click button (//div[@Class="example"]/button)
  • Retrieve elementHandle(//div[@Class="elements"])
  • Subquery this one using elementHandle.$('//button')
  • Click this element
    => Add element button is clicked instead of Delete
@mxschmitt
Copy link
Member

I tried to reproduce it with the following example which seems to work as expected, once I move it inside the div, I get two results and otherwise 1 result: https://try.playwright.tech/?s=evnej1h

Could you provide us a full example?

@sarut0bi
Copy link
Author

it seems to be appear only when i using xpath selectors:

// @ts-check
const playwright = require("playwright");

(async () => {
  const browser = await playwright.chromium.launch();
  const context = await browser.newContext();
  const page = await context.newPage();
  await page.setContent(`
  <span class="find-me">1</span>
  <div id="a1">
    <span class="find-me">2</span>
  </div>`)
  const baseElement = await page.$(`//*[@id="a1"]`);
  let element = await baseElement.$(`//*[@class="find-me"]`);
  console.log(await element.innerText())
  await browser.close();
})();

@pavelfeldman
Copy link
Member

This isn't a bug formally, because instead of .// that starts search from the current node, you explicitly requested // that resets search to the root of the document. But I can see how this can be confusing. We should probably treat // as .// in this method.

@dgozman dgozman changed the title elementHandle.$ query in whole page instead of the given element [Feature] elementHandle.$ query in whole page instead of the given element Feb 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants