playwright icon indicating copy to clipboard operation
playwright copied to clipboard

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

Open sarut0bi opened this issue 5 years ago • 3 comments

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

sarut0bi avatar Jan 21 '21 07:01 sarut0bi

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?

mxschmitt avatar Jan 21 '21 11:01 mxschmitt

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();
})();

sarut0bi avatar Jan 21 '21 13:01 sarut0bi

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.

pavelfeldman avatar Jan 21 '21 17:01 pavelfeldman