skip to Main Content

I spent hours trying to figure out how to solve the following.

I am automating a test case with Playwright – JavaScript where I have to upload an image but it gives me all kinds of errors, the last one I am trying to test is with the documentation uploaded in playwright.dev

Test:
const path = require(‘path’);
import { test, expect } from ‘@playwright/test’;

test("s", async ({ page }) => {
    await page.goto('https://www.transfernow.net/es');
    await page.getByRole('button', { name: 'Inicio' }).click();
    await page.getByRole('button', { name: 'Inicio' }).first().setInputFiles(path.join(__dirname, 'images.png'));
 }); 

Error:

Error: locator.setInputFiles: Error: Node is not an HTMLInputElement
Call log:

  • waiting for getByRole(‘button’, { name: ‘Inicio’ }).first()
  • locator resolved to …

How i can fix this?.

2

Answers


  1. As mentioned in the comment , please make sure that you are using the setInputFiles() method on an input element of type "file" that is visible and enabled.

    Login or Signup to reply.
  2. As per the Error: Node is not an HTMLInputElement

    Indicates that setInputFiles method is being called on a node that is not an HTML input element of type file. In order to fix this, you need to ensure that you are targeting the correct file input element on the webpage.

    Refer the code below for reference :

    import { test, expect } from '@playwright/test';
    const path = require('path');
    
    test("Upload image", async ({ page }) => {
        await page.goto('https://www.transfernow.net/es');  // navigate to your site    
        await page.getByRole('button', { name: 'Inicio' }).click(); // Click button    
        const fileInput = await page.$('input[type="file"]'); // Locate the file   
        if (fileInput) {      
            const filePath = path.join(__dirname, 'images.png'); // Set the file to upload
            await fileInput.setInputFiles(filePath);
        } else {
            console.error('File input element not found');
        }
       // any other action you needs to do.. comes here
    });
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search