question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I run an xpath expression?

See original GitHub issue

Hi everyone,

I checked the docs here but it’s not clear to me whether there’s a way to make the class Runtime evaluate not just JavaScript expressions but also XPaths.

Has anyone tried?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
cyrus-andcommented, Jun 12, 2017

I think what you want is DOM.performSearch:

const CDP = require('chrome-remote-interface');

CDP(async (client) => {
    try {
        // extract and enable domains
        const {DOM, Page} = client;
        await DOM.enable();
        await Page.enable();
        // navigate the page
        await Page.navigate({url: 'http://example.com'});
        await Page.loadEventFired();
        // "It is important that client receives DOM events only for the nodes
        // that are known to the client."
        await DOM.getDocument();
        // perform the XPath search query
        const {searchId, resultCount} = await DOM.performSearch({
            query: '/html/body/div/*'
        });
        // fetch and display all the results
        const {nodeIds} = await DOM.getSearchResults({
            searchId,
            fromIndex: 0,
            toIndex: resultCount
        });
        console.log(nodeIds);
    } catch (err) {
        console.error(err);
    } finally {
        client.close();
    }
}).on('error', (err) => {
    console.error(err);
});

Otherwise you can always use Runtime.evaluate to inject a Document.evaluate call.

6reactions
tsirolnikcommented, Mar 28, 2018

I would like to note that WITHOUT calling DOM.getDocument prior to performing the search results, the output of getSearchResults will be an array with zeros.

Code:

        // Removing this line will cause getSearchResults to return an array filled with zeros.
         await DOM.getDocument();
        let { searchId, resultCount } = await DOM.performSearch({ query: xpath });
        if (resultCount < 1) return null;
        let { nodeIds } = await tDOM.getSearchResults({
            searchId,
            fromIndex: 0,
            toIndex: resultCount
        });
        console.log(nodeIds);
        nodeIds.forEach(async id => {
            // This will fail without DOM.getDocument
            console.log(await DOM.getAttributes({ nodeId: id })); 
        })

Output without getDocument - image

Output with getDocument - image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with XPath Expressions - Oxygen XML Editor
Note: If an XPath expression is run over a JSON document, it is converted to XML and the XPath is executed over the...
Read more >
XPath - Expression - Tutorialspoint
XPath uses a path expression to select node or a list of nodes from an XML document. Following is the list of useful...
Read more >
XPath Tutorial - W3Schools
XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the path expressions...
Read more >
Use Visual C# to query XML with an XPath expression
This article introduces how to query an XPathDocument object with an XML Path Language (XPath) expression by using the XPathNavigator class.
Read more >
XPath expression evaluation | IntelliJ IDEA Documentation
Evaluate an XPath expression · Choose Evaluate XPath from the context menu of the active editor tab or go to Edit | Find...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found