Search code examples
javascriptplaywright

Playwright JavaScript - Upload image


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?.


Solution

  • 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.