Skip to content

fix: file upload on input elements with display:none style #154

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/__tests__/tools/browser/interaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ describe('Browser Interaction Tools', () => {

const result = await uploadFileTool.execute(args, mockContext);

expect(mockPageWaitForSelector).toHaveBeenCalledWith('#file-input');
expect(mockPageWaitForSelector).toHaveBeenCalledWith('#file-input', {"state": "attached"});
expect(mockPageSetInputFiles).toHaveBeenCalledWith('#file-input', '/tmp/testfile.txt');
expect(result.isError).toBe(false);
expect(result.content[0].text).toContain("Uploaded file '/tmp/testfile.txt' to '#file-input'");
Expand Down
32 changes: 26 additions & 6 deletions src/tools/browser/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,34 @@ export class HoverTool extends BrowserToolBase {
* Tool for uploading files
*/
export class UploadFileTool extends BrowserToolBase {
/**
* Execute the upload file tool
*/
async execute(args: any, context: ToolContext): Promise<ToolResponse> {
return this.safeExecute(context, async (page) => {
await page.waitForSelector(args.selector);
await page.setInputFiles(args.selector, args.filePath);
return createSuccessResponse(`Uploaded file '${args.filePath}' to '${args.selector}'`);
await page.waitForSelector(args.selector, { state: 'attached' });

// Only make the input visible if it is currently display:none
await page.evaluate((selector) => {
const el = document.querySelector(selector) as HTMLElement | null;
if (el) {
const style = window.getComputedStyle(el);
if (style.display === 'none') {
el.setAttribute('data-upload-temp-visible', 'true');
el.style.display = 'block';
}
}
}, args.selector);

await page.setInputFiles(args.selector, args.filePath);

// Make the input invisible again only if we made it visible
await page.evaluate((selector) => {
const el = document.querySelector(selector) as HTMLElement | null;
if (el && el.getAttribute('data-upload-temp-visible') === 'true') {
el.style.display = 'none';
el.removeAttribute('data-upload-temp-visible');
}
}, args.selector);

return createSuccessResponse(`Uploaded file '${args.filePath}' to '${args.selector}'`);
});
}
}
Expand Down
Loading