mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
doc(dotnet): add a self-contained example (#6702)
This commit is contained in:
parent
ba29e99ace
commit
f9357531f5
@ -65,7 +65,7 @@ with sync_playwright() as playwright:
|
|||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
class Example
|
class Program
|
||||||
{
|
{
|
||||||
public static async Task Main()
|
public static async Task Main()
|
||||||
{
|
{
|
||||||
|
@ -1534,19 +1534,19 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
class FrameExamples
|
class FrameExamples
|
||||||
{
|
{
|
||||||
public static async Task Main()
|
public static async Task Main()
|
||||||
{
|
|
||||||
using var playwright = await Playwright.CreateAsync();
|
|
||||||
await using var browser = await playwright.Chromium.LaunchAsync();
|
|
||||||
var page = await browser.NewPageAsync();
|
|
||||||
|
|
||||||
foreach (var currentUrl in new[] { "https://www.google.com", "https://bbc.com" })
|
|
||||||
{
|
{
|
||||||
await page.GotoAsync(currentUrl);
|
using var playwright = await Playwright.CreateAsync();
|
||||||
element = await page.MainFrame.WaitForSelectorAsync("img");
|
await using var browser = await playwright.Chromium.LaunchAsync();
|
||||||
Console.WriteLine($"Loaded image: {await element.GetAttributeAsync("src")}");
|
var page = await browser.NewPageAsync();
|
||||||
|
|
||||||
|
foreach (var currentUrl in new[] { "https://www.google.com", "https://bbc.com" })
|
||||||
|
{
|
||||||
|
await page.GotoAsync(currentUrl);
|
||||||
|
element = await page.MainFrame.WaitForSelectorAsync("img");
|
||||||
|
Console.WriteLine($"Loaded image: {await element.GetAttributeAsync("src")}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -1406,26 +1406,26 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
class PageExamples
|
class PageExamples
|
||||||
{
|
{
|
||||||
public static async Task Main()
|
public static async Task Main()
|
||||||
{
|
{
|
||||||
using var playwright = await Playwright.CreateAsync();
|
using var playwright = await Playwright.CreateAsync();
|
||||||
await using var browser = await playwright.Webkit.LaunchAsync(new BrowserTypeLaunchOptions
|
await using var browser = await playwright.Webkit.LaunchAsync(new BrowserTypeLaunchOptions
|
||||||
{
|
{
|
||||||
Headless: false
|
Headless: false
|
||||||
});
|
});
|
||||||
var page = await browser.NewPageAsync();
|
var page = await browser.NewPageAsync();
|
||||||
|
|
||||||
await page.ExposeBindingAsync("pageUrl", (source) => source.Page.Url);
|
await page.ExposeBindingAsync("pageUrl", (source) => source.Page.Url);
|
||||||
await page.SetContentAsync("<script>\n" +
|
await page.SetContentAsync("<script>\n" +
|
||||||
" async function onClick() {\n" +
|
" async function onClick() {\n" +
|
||||||
" document.querySelector('div').textContent = await window.pageURL();\n" +
|
" document.querySelector('div').textContent = await window.pageURL();\n" +
|
||||||
" }\n" +
|
" }\n" +
|
||||||
"</script>\n" +
|
"</script>\n" +
|
||||||
"<button onclick=\"onClick()\">Click me</button>\n" +
|
"<button onclick=\"onClick()\">Click me</button>\n" +
|
||||||
"<div></div>");
|
"<div></div>");
|
||||||
|
|
||||||
await page.ClickAsync("button");
|
await page.ClickAsync("button");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -1664,34 +1664,34 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
class PageExamples
|
class PageExamples
|
||||||
{
|
{
|
||||||
public static async Task Main()
|
public static async Task Main()
|
||||||
{
|
{
|
||||||
using var playwright = await Playwright.CreateAsync();
|
using var playwright = await Playwright.CreateAsync();
|
||||||
await using var browser = await playwright.Webkit.LaunchAsync(new BrowserTypeLaunchOptions
|
await using var browser = await playwright.Webkit.LaunchAsync(new BrowserTypeLaunchOptions
|
||||||
{
|
{
|
||||||
Headless: false
|
Headless: false
|
||||||
});
|
});
|
||||||
var page = await browser.NewPageAsync();
|
var page = await browser.NewPageAsync();
|
||||||
|
|
||||||
// NOTE: md5 is inherently insecure, and we strongly discourage using
|
// NOTE: md5 is inherently insecure, and we strongly discourage using
|
||||||
// this in production in any shape or form
|
// this in production in any shape or form
|
||||||
await page.ExposeFunctionAsync("sha1", (string input) =>
|
await page.ExposeFunctionAsync("sha1", (string input) =>
|
||||||
{
|
{
|
||||||
return Convert.ToBase64String(
|
return Convert.ToBase64String(
|
||||||
MD5.Create().ComputeHash(System.Text.Encoding.UTF8.GetBytes(input)));
|
MD5.Create().ComputeHash(System.Text.Encoding.UTF8.GetBytes(input)));
|
||||||
});
|
});
|
||||||
|
|
||||||
await page.SetContentAsync("<script>\n" +
|
await page.SetContentAsync("<script>\n" +
|
||||||
" async function onClick() {\n" +
|
" async function onClick() {\n" +
|
||||||
" document.querySelector('div').textContent = await window.sha1('PLAYWRIGHT');\n" +
|
" document.querySelector('div').textContent = await window.sha1('PLAYWRIGHT');\n" +
|
||||||
" }\n" +
|
" }\n" +
|
||||||
"</script>\n" +
|
"</script>\n" +
|
||||||
"<button onclick=\"onClick()\">Click me</button>\n" +
|
"<button onclick=\"onClick()\">Click me</button>\n" +
|
||||||
"<div></div>");
|
"<div></div>");
|
||||||
|
|
||||||
await page.ClickAsync("button");
|
await page.ClickAsync("button");
|
||||||
Console.WriteLine(await page.TextContentAsync("div"));
|
Console.WriteLine(await page.TextContentAsync("div"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -332,17 +332,17 @@ with sync_playwright() as p:
|
|||||||
```csharp
|
```csharp
|
||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
|
|
||||||
class Example
|
class Program
|
||||||
{
|
{
|
||||||
public async void Main()
|
public static async Task Main()
|
||||||
{
|
{
|
||||||
using var playwright = await Playwright.CreateAsync();
|
using var playwright = await Playwright.CreateAsync();
|
||||||
var chromium = playwright.Chromium;
|
var chromium = playwright.Chromium;
|
||||||
var context = chromium.LaunchPersistentContextAsync(@"C:\path\to\directory\", new BrowserTypeLaunchPersistentContextOptions
|
var context = chromium.LaunchPersistentContextAsync(@"C:\path\to\directory\", new BrowserTypeLaunchPersistentContextOptions
|
||||||
{
|
{
|
||||||
Headless = false
|
Headless = false
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -71,7 +71,7 @@ browser = playwright.chromium.launch(channel="chrome")
|
|||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
class Example
|
class Program
|
||||||
{
|
{
|
||||||
public static async Task Main()
|
public static async Task Main()
|
||||||
{
|
{
|
||||||
|
@ -270,9 +270,9 @@ browser = playwright.chromium.launch(chromiumSandbox=False)
|
|||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
class Example
|
class Program
|
||||||
{
|
{
|
||||||
public async void Main()
|
public static async Task Main()
|
||||||
{
|
{
|
||||||
using var playwright = await Playwright.CreateAsync();
|
using var playwright = await Playwright.CreateAsync();
|
||||||
await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
|
await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
|
||||||
@ -399,9 +399,9 @@ with sync_playwright() as p:
|
|||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
class Example
|
class Program
|
||||||
{
|
{
|
||||||
public async void Main()
|
public static async Task Main()
|
||||||
{
|
{
|
||||||
using var playwright = await Playwright.CreateAsync();
|
using var playwright = await Playwright.CreateAsync();
|
||||||
await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
|
await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
|
||||||
|
@ -172,7 +172,7 @@ with sync_playwright() as p:
|
|||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
class Example
|
class Program
|
||||||
{
|
{
|
||||||
public static async Task Main()
|
public static async Task Main()
|
||||||
{
|
{
|
||||||
|
@ -67,7 +67,7 @@ with sync_playwright() as p:
|
|||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
class Example
|
class Program
|
||||||
{
|
{
|
||||||
public static async Task Main()
|
public static async Task Main()
|
||||||
{
|
{
|
||||||
|
@ -67,18 +67,18 @@ with sync_playwright() as playwright:
|
|||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
class Example
|
class Program
|
||||||
{
|
{
|
||||||
public async void Main()
|
public static async Task Main()
|
||||||
{
|
{
|
||||||
using var playwright = await Playwright.CreateAsync();
|
using var playwright = await Playwright.CreateAsync();
|
||||||
await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
|
await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
|
||||||
{
|
{
|
||||||
Headless: False
|
Headless: False
|
||||||
});
|
});
|
||||||
var pixel2 = playwright.Devices["Pixel 2"];
|
var pixel2 = playwright.Devices["Pixel 2"];
|
||||||
await using var context = await browser.NewContextAsync(pixel2);
|
await using var context = await browser.NewContextAsync(pixel2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ dotnet add package Microsoft.Playwright
|
|||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
class Example
|
class Program
|
||||||
{
|
{
|
||||||
public static async Task Main()
|
public static async Task Main()
|
||||||
{
|
{
|
||||||
@ -33,26 +33,41 @@ class Example
|
|||||||
|
|
||||||
## First script
|
## First script
|
||||||
|
|
||||||
In our first script, we will navigate to `whatsmyuseragent.org` and take a screenshot in WebKit.
|
Create a console project and add the Playwright dependency.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
dotnet new console -n pw_demo
|
||||||
|
cd pw_demo
|
||||||
|
dotnet add package Microsoft.Playwright --prerelease
|
||||||
|
```
|
||||||
|
|
||||||
|
Create a Program.cs that will navigate to `https://playwright.dev/dotnet` and take a screenshot in Chromium.
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
class Example
|
class Program
|
||||||
{
|
{
|
||||||
public static async Task Main()
|
public static async Task Main()
|
||||||
{
|
{
|
||||||
using var playwright = await Playwright.CreateAsync();
|
using var playwright = await Playwright.CreateAsync();
|
||||||
await using var browser = await playwright.Chromium.LaunchAsync();
|
await using var browser = await playwright.Chromium.LaunchAsync();
|
||||||
var page = await browser.NewPageAsync();
|
var page = await browser.NewPageAsync();
|
||||||
await page.GotoAsync("whatsmyuseragent.org");
|
await page.GotoAsync("https://playwright.dev/dotnet");
|
||||||
await page.ScreenshotAsync(new PageScreenshotOptions { Path = "screenshot.png" });
|
await page.ScreenshotAsync(new PageScreenshotOptions { Path = "screenshot.png" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
By default, Playwright runs the browsers in headless mode. To see the browser UI, pass the `headless: false` flag while launching the browser. You can also use [`option: slowMo`] to slow down execution. Learn more in the debugging tools [section](./debug.md).
|
Now build it and run it.
|
||||||
|
|
||||||
|
```ssh
|
||||||
|
dotnet build
|
||||||
|
dotnet run
|
||||||
|
```
|
||||||
|
|
||||||
|
By default, Playwright runs the browsers in headless mode. To see the browser UI, pass the `Headless = false` flag while launching the browser. You can also use [`option: slowMo`] to slow down execution. Learn more in the debugging tools [section](./debug.md).
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
await playwright.Firefox.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false, SlowMo = 50 });
|
await playwright.Firefox.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false, SlowMo = 50 });
|
||||||
|
@ -87,7 +87,7 @@ with sync_playwright() as playwright:
|
|||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
class Example
|
class Program
|
||||||
{
|
{
|
||||||
public static async Task Main()
|
public static async Task Main()
|
||||||
{
|
{
|
||||||
|
@ -243,17 +243,17 @@ with sync_playwright() as playwright:
|
|||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
class Example
|
class Program
|
||||||
{
|
{
|
||||||
public async void Main()
|
public static async Task Main()
|
||||||
{
|
{
|
||||||
using var playwright = await Playwright.CreateAsync();
|
using var playwright = await Playwright.CreateAsync();
|
||||||
await using var browser = await playwright.Chromium.LaunchAsync();
|
await using var browser = await playwright.Chromium.LaunchAsync();
|
||||||
var page = await browser.NewPageAsync();
|
var page = await browser.NewPageAsync();
|
||||||
page.Request += (_, request) => Console.WriteLine(">> " + request.Method + " " + request.Url);
|
page.Request += (_, request) => Console.WriteLine(">> " + request.Method + " " + request.Url);
|
||||||
page.Response += (_, response) => Console.WriteLine("<<" + response.Status + " " + response.Url);
|
page.Response += (_, response) => Console.WriteLine("<<" + response.Status + " " + response.Url);
|
||||||
await page.GotoAsync("https://example.com");
|
await page.GotoAsync("https://example.com");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user