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 Request] Return All Nodes from XPath #1705

Closed
alixaxel opened this issue Jan 2, 2018 · 2 comments
Closed

[Feature Request] Return All Nodes from XPath #1705

alixaxel opened this issue Jan 2, 2018 · 2 comments
Assignees

Comments

@alixaxel
Copy link
Contributor

alixaxel commented Jan 2, 2018

Right now, the current page.xpath() implementation only returns the first matched element.

I find this implementation somewhat restrictive, specially considering it's tied to ElementHandle.

For instance, if we wish to find all ANY_* XPath result type constants on the MDN website:

id("wikiArticle")/table[@class="standard-table"]//td[1]/code[starts-with(., 'ANY_')]

The above XPath would do the job, but with the current implementation we are forced to either traverse the DOM using good old page.evaluate() or known how many elements would be matched beforehand and issue n separate XPath queries in the form of (expression)[1..n]. Or just run XPath in page.evaluate:

await page.evaluate(() => {
  function $x(expression) {
    let result = [];
    let element = null;
    let elements = document.evaluate(expression, document, null, XPathResult.ANY_TYPE, null);

    while ((element = elements.iterateNext()) !== null) {
      result.push(element);
    }

    return result;
  }

  return $x('id("wikiArticle")/table[@class="standard-table"]//td[1]/code[starts-with(., "ANY_")]').map(
    (element) => {
      return element.textContent;
    }
  );
});

// ["ANY_TYPE", "ANY_UNORDERED_NODE_TYPE"]

It would be great to have more capable XPath querying abilities before PPTR 1.0.0 is released.

@JoelEinbinder JoelEinbinder self-assigned this Jan 2, 2018
@aslushnikov
Copy link
Contributor

@JoelEinbinder how about returning array of all matched elements from page.xpath?

@yujiosaka
Copy link

FYI

selenium-webdriver has both getAttributeByXpath and getAttributesByXpath.
the former returns a single element and the latter returns all.

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

No branches or pull requests

4 participants