Playwright文档 - FileChooser(文件选择器)


FileChooser对象由页面在page.on('filechooser')事件中调度。

class: FileChooser

  • since: v1.8

[FileChooser] objects are dispatched by the page in the [event: Page.fileChooser] event.

1// Start waiting for file chooser before clicking. Note no await. 2const fileChooserPromise = page.waitForEvent('filechooser'); 3await page.getByText('Upload file').click(); 4const fileChooser = await fileChooserPromise; 5await fileChooser.setFiles(path.join(__dirname, 'myfile.pdf')); 6
1FileChooser fileChooser = page.waitForFileChooser(() -> page.getByText("Upload file").click()); 2fileChooser.setFiles(Paths.get("myfile.pdf")); 3
1async with page.expect_file_chooser() as fc_info: 2 await page.get_by_text("Upload file").click() 3file_chooser = await fc_info.value 4await file_chooser.set_files("myfile.pdf") 5
1with page.expect_file_chooser() as fc_info: 2 page.get_by_text("Upload file").click() 3file_chooser = fc_info.value 4file_chooser.set_files("myfile.pdf") 5
1var fileChooser = await page.RunAndWaitForFileChooserAsync(async () => 2{ 3 await page.GetByText("Upload file").ClickAsync(); 4}); 5await fileChooser.SetFilesAsync("temp.txt"); 6

method: FileChooser.element

  • since: v1.8
  • returns: <[ElementHandle]>

Returns input element associated with this file chooser.

method: FileChooser.isMultiple

  • since: v1.8
  • returns: <[boolean]>

Returns whether this file chooser accepts multiple files.

method: FileChooser.page

  • since: v1.8
  • returns: <[Page]>

Returns page this file chooser belongs to.

async method: FileChooser.setFiles

  • since: v1.8

Sets the value of the file input this chooser is associated with. If some of the filePaths are relative paths, then they are resolved relative to the current working directory. For empty array, clears the selected files.

param: FileChooser.setFiles.files = %%-input-files-%%

  • since: v1.8

option: FileChooser.setFiles.noWaitAfter = %%-input-no-wait-after-%%

  • since: v1.8

option: FileChooser.setFiles.timeout = %%-input-timeout-%%

  • since: v1.8

option: FileChooser.setFiles.timeout = %%-input-timeout-js-%%

  • since: v1.8