playwright/docs/src/cheat-sheet.md
2021-06-10 13:43:42 -07:00

5.5 KiB

id title
cheat-sheet Cheat Sheet

Download & Upload

Download file

const [ download ] = await Promise.all([
  page.waitForEvent('download'),
  page.click('button')
]);
const path = await download.path();
async with page.expect_download() as download_info:
    await page.click("button")
download = await download_info.value
path = await download.path()
with page.expect_download() as download_info:
    page.click("button")
download = download_info.value
path = download.path()
var waitForDownloadTask = page.WaitForDownloadAsync();
await page.ClickAsync("#downloadButton");
var download = await waitForDownloadTask;
var path = await download.PathAsync();

Learn more

Upload file

await page.setInputFiles('input#upload', 'myfile.pdf');
page.setInputFiles("input#upload", Paths.get("myfile.pdf"));
await page.set_input_files('input#upload', 'myfile.pdf')
page.set_input_files('input#upload', 'myfile.pdf')
await page.SetInputFilesAsync("input#upload", "myfile.pdf");

Learn more

Upload multiple files

await page.setInputFiles('input#upload', ['file1.txt', 'file2.txt']);
page.setInputFiles("input#upload", new Path[] {Paths.get("file1.txt"), Paths.get("file2.txt")});
await page.set_input_files('input#upload', ['file1.txt', 'file2.txt'])
page.set_input_files('input#upload', ['file1.txt', 'file2.txt'])
await page.SetInputFilesAsync("input#upload", new[] { "file1.txt", "file12.txt" });

Learn more

Upload from memory

await page.setInputFiles('input#upload', {
  name: 'file.txt',
  mimeType: 'text/plain',
  buffer: Buffer.from('this is test')
});
page.setInputFiles("input#upload", new FilePayload(
  "file.txt", "text/plain", "this is test".getBytes(StandardCharsets.UTF_8)));
await page.set_input_files(
    "input#upload",
    files=[
        {"name": "test.txt", "mimeType": "text/plain", "buffer": b"this is a test"}
    ],
)
page.set_input_files(
    "input#upload",
    files=[
        {"name": "test.txt", "mimeType": "text/plain", "buffer": b"this is a test"}
    ],
)
await page.SetInputFilesAsync("input#upload", new FilePayload
{
    Name = "file.txt",
    MimeType = "text/plain",
    Buffer = "this is a test".getBytes(StandardCharsets.UTF_8),
});

Learn more

Remove selected files

await page.setInputFiles('input#upload', []);
page.setInputFiles("input#upload", new Path[0]);
await page.set_input_files('input#upload', [])
page.set_input_files('input#upload', [])
await page.SetInputFilesAsync("input#upload", new[] {});

Learn more

Handle file picker

const [fileChooser] = await Promise.all([
  page.waitForEvent('filechooser'),
  page.click('upload')
]);
await fileChooser.setFiles('myfile.pdf');
FileChooser fileChooser = page.waitForFileChooser(() -> {
  page.click("upload");
});
fileChooser.setFiles(Paths.get("myfile.pdf"));
async with page.expect_file_chooser() as fc_info:
    await page.click("upload")
file_chooser = await fc_info.value
await file_chooser.set_files("myfile.pdf")
with page.expect_file_chooser() as fc_info:
    page.click("upload")
file_chooser = fc_info.value
file_chooser.set_files("myfile.pdf")
var fileChooser = page.RunAndWaitForFileChooserAsync(async () =>
{
    await page.ClickAsync("upload");
});
await fileChooser.SetFilesAsync("myfile.pdf");

Learn more

Manage &#60iframe&#62s

List frames

const frames = page.frames();
List<Frame> frames = page.frames();
frames = page.frames
frames = page.frames
var frame = page.Frames;

Learn more

Frame by name attribute

const frame = page.frame('frame-login');
Frame frame = page.frame("frame-login");
frame = page.frame('frame-login')
frame = page.frame('frame-login')
var frame = page.Frame("frame-login");

Learn more

Frame by URL

const frame = page.frame({ url: /.*domain.*/ });
Frame frame = page.frameByUrl(Pattern.compile(".*domain.*"));
frame = page.frame(url=r'.*domain.*')
frame = page.frame(url=r'.*domain.*')
var frame = page.FrameByUrl("*domain.");

Learn more

Frame by selector

const frameElementHandle = await page.$('.frame-class');
const frame = await frameElementHandle.contentFrame();
ElementHandle frameElementHandle = page.querySelector(".frame-class");
Frame frame = frameElementHandle.contentFrame();
frame_element_handle = await page.query_selector('.frame-class')
frame = await frame_element_handle.content_frame()
frame_element_handle = page.query_selector('.frame-class')
frame = frame_element_handle.content_frame()
var frameElementHandle = await page.QuerySelectorAsync(".frame-class");
var frame = await frameElementHandle.ContentFrameAsync();

Learn more