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

How do I upload files? #164

Closed
SunGg12138 opened this issue Jun 12, 2017 · 3 comments
Closed

How do I upload files? #164

SunGg12138 opened this issue Jun 12, 2017 · 3 comments

Comments

@SunGg12138
Copy link

SunGg12138 commented Jun 12, 2017

How do I upload files?

@cyrus-and
Copy link
Owner

Hm, do you mean how to submit a form which include file upload using the CDP? Specifically how to handle the <input type="file" /> input tag?

In that case, you can use DOM.setFileInputFiles:

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

CDP(async (client) => {
    try {
        const {DOM, Runtime} = client;
        // fetch the file input node
        const {root} =  await DOM.getDocument();
        const {nodeId: myFileId} = await DOM.querySelector({
            nodeId: root.nodeId,
            selector: '#myFile'
        });
        // fill the file input
        await DOM.setFileInputFiles({
            nodeId: myFileId,
            files: ['/tmp/foo', '/tmp/bar']
        });
        // submit the form
        await Runtime.evaluate({
            expression: `document.getElementById('myForm').submit()`
        });
    } catch (err) {
        console.error(err);
    } finally {
        client.close();
    }
}).on('error', (err) => {
    console.error(err);
});

@SunGg12138
Copy link
Author

Thank you! Your answer is perfect!

@zhangLeLer
Copy link

May I ask how to access documents in an iframe, such as accessing a div element that exists in the iframe

Thank you! Your answer is perfect!

May I ask how to access documents in an iframe, such as accessing a div element that exists in the iframe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants