@@ -137,14 +137,34 @@ export class HoverTool extends BrowserToolBase {
137137 * Tool for uploading files
138138 */
139139export class UploadFileTool extends BrowserToolBase {
140- /**
141- * Execute the upload file tool
142- */
143140 async execute ( args : any , context : ToolContext ) : Promise < ToolResponse > {
144141 return this . safeExecute ( context , async ( page ) => {
145- await page . waitForSelector ( args . selector ) ;
146- await page . setInputFiles ( args . selector , args . filePath ) ;
147- return createSuccessResponse ( `Uploaded file '${ args . filePath } ' to '${ args . selector } '` ) ;
142+ await page . waitForSelector ( args . selector , { state : 'attached' } ) ;
143+
144+ // Only make the input visible if it is currently display:none
145+ await page . evaluate ( ( selector ) => {
146+ const el = document . querySelector ( selector ) as HTMLElement | null ;
147+ if ( el ) {
148+ const style = window . getComputedStyle ( el ) ;
149+ if ( style . display === 'none' ) {
150+ el . setAttribute ( 'data-upload-temp-visible' , 'true' ) ;
151+ el . style . display = 'block' ;
152+ }
153+ }
154+ } , args . selector ) ;
155+
156+ await page . setInputFiles ( args . selector , args . filePath ) ;
157+
158+ // Make the input invisible again only if we made it visible
159+ await page . evaluate ( ( selector ) => {
160+ const el = document . querySelector ( selector ) as HTMLElement | null ;
161+ if ( el && el . getAttribute ( 'data-upload-temp-visible' ) === 'true' ) {
162+ el . style . display = 'none' ;
163+ el . removeAttribute ( 'data-upload-temp-visible' ) ;
164+ }
165+ } , args . selector ) ;
166+
167+ return createSuccessResponse ( `Uploaded file '${ args . filePath } ' to '${ args . selector } '` ) ;
148168 } ) ;
149169 }
150170}
0 commit comments