Best Playwright-dotnet code snippet using Microsoft.Playwright.LocatorTextContentOptions
ILocator.cs
Source: ILocator.cs
...868 /// <param name="options">Call options</param>869 Task TapAsync(LocatorTapOptions? options = default);870 /// <summary><para>Returns the <c>node.textContent</c>.</para></summary>871 /// <param name="options">Call options</param>872 Task<string?> TextContentAsync(LocatorTextContentOptions? options = default);873 /// <summary>874 /// <para>875 /// Focuses the element, and then sends a <c>keydown</c>, <c>keypress</c>/<c>input</c>,876 /// and <c>keyup</c> event for each character in the text.877 /// </para>878 /// <para>To press a special key, like <c>Control</c> or <c>ArrowDown</c>, use <see cref="ILocator.PressAsync"/>.</para>879 /// <code>880 /// await element.TypeAsync("Hello"); // Types instantly<br/>881 /// await element.TypeAsync("World", delay: 100); // Types slower, like a user882 /// </code>883 /// <para>An example of typing into a text field and then submitting the form:</para>884 /// <code>885 /// var element = page.Locator("input");<br/>886 /// await element.TypeAsync("some text");<br/>...
LocatorSynchronous.cs
Source: LocatorSynchronous.cs
...763 return locator.InputValueAsync(options).GetAwaiter().GetResult();764 }765 /// <summary><para>Returns the <c>node.textContent</c>.</para></summary>766 /// <param name="options">Call options</param>767 public static string? TextContent(this ILocator locator, LocatorTextContentOptions? options = null)768 {769 return locator.TextContentAsync(options).GetAwaiter().GetResult();770 }771 /// <summary><para>Returns an array of <c>node.innerText</c> values for all matching nodes.</para></summary>772 public static IReadOnlyList<string> AllInnerTexts(this ILocator locator)773 {774 return locator.AllInnerTextsAsync().GetAwaiter().GetResult();775 }776 /// <summary><para>Returns an array of <c>node.textContent</c> values for all matching nodes.</para></summary>777 public static IReadOnlyList<string> AllTextContents(this ILocator locator)778 {779 return locator.AllTextContentsAsync().GetAwaiter().GetResult();780 }781 /// <summary>...
Locator.cs
Source: Locator.cs
...177 public Task SetInputFilesAsync(IEnumerable<FilePayload> files, LocatorSetInputFilesOptions options = null)178 => _frame.SetInputFilesAsync(_selector, files, ConvertOptions<FrameSetInputFilesOptions>(options));179 public Task TapAsync(LocatorTapOptions options = null)180 => _frame.TapAsync(_selector, ConvertOptions<FrameTapOptions>(options));181 public Task<string> TextContentAsync(LocatorTextContentOptions options = null)182 => _frame.TextContentAsync(_selector, ConvertOptions<FrameTextContentOptions>(options));183 public Task TypeAsync(string text, LocatorTypeOptions options = null)184 => _frame.TypeAsync(_selector, text, ConvertOptions<FrameTypeOptions>(options));185 public Task UncheckAsync(LocatorUncheckOptions options = null)186 => _frame.UncheckAsync(_selector, ConvertOptions<FrameUncheckOptions>(options));187 ILocator ILocator.Locator(string selector, LocatorLocatorOptions options)188 => new Locator(_frame, $"{_selector} >> {selector}", options);189 public Task WaitForAsync(LocatorWaitForOptions options = null)190 => _frame.LocatorWaitForAsync(_selector, ConvertOptions<LocatorWaitForOptions>(options));191 internal Task<FrameExpectResult> ExpectAsync(string expression, FrameExpectOptions options = null)192 => _frame.ExpectAsync(193 _selector,194 expression,195 options);...
PlaywrightSyncElement.cs
Source: PlaywrightSyncElement.cs
...308 {309 return ElementLocator().GetAttributeAsync(name, options).Result;310 }311 /// <inheritdoc cref = "ILocator.TextContentAsync" />312 public string? TextContent(LocatorTextContentOptions? options = null)313 {314 return ElementLocator().TextContentAsync(options).Result;315 }316 /// <inheritdoc cref = "ILocator.AllTextContentsAsync" />317 public IReadOnlyList<string> AllTextContents()318 {319 return ElementLocator().AllTextContentsAsync().Result;320 }321 /// <inheritdoc cref = "ILocator.AllInnerTextsAsync" />322 public IReadOnlyList<string> AllInnerTexts()323 {324 return ElementLocator().AllInnerTextsAsync().Result;325 }326 /// <inheritdoc cref = "ILocator.InnerHTMLAsync" />...
LocatorAssertions.cs
Source: LocatorAssertions.cs
...237 public static ILocator HaveTextContent(238 this ReferenceTypeAssertion<ILocator> locator,239 string expected,240 string because = "no reason given",241 LocatorTextContentOptions? textContentOptions = null)242 {243 var element = locator.Value;244 var actual = element.TextContent(textContentOptions) ?? "";245 if (string.Compare(actual, expected) != 0)246 {247 throw new AssertException(@$"248BeTextContent Assert Exception249Actual:250{actual}251Expected:252{expected}253Because:254{because}255");256 }257 return element;258 }259 public static ILocator HaveNotTextContent(260 this ReferenceTypeAssertion<ILocator> locator,261 string notExpected,262 string because = "no reason given",263 LocatorTextContentOptions? textContentOptions = null)264 {265 var element = locator.Value;266 var actual = element.TextContent(textContentOptions) ?? "";267 if (string.Compare(actual, notExpected) == 0)268 {269 throw new AssertException(@$"270BeNotTextContent Assert Exception271Not expected:272{notExpected}273Actual:274{actual}275Because:276{because}277");...
Handlers.cs
Source: Handlers.cs
...31 await page.GotoAsync(url, new PageGotoOptions{32 WaitUntil = WaitUntilState.NetworkIdle33 });34 string? username = null;35 try { username = await page.Locator(".e1e1d > div:nth-child(1) > a:nth-child(1)").TextContentAsync(new LocatorTextContentOptions { Timeout = 1000 }); } catch {}36 if (string.IsNullOrEmpty(username)) username = "unknown";37 if (url.Contains("/p/")) {38 // goto page39 await page.GotoAsync("https://igdownloader.com/", new PageGotoOptions{40 WaitUntil = WaitUntilState.NetworkIdle41 });42 // search for post43 await page.TypeAsync(".ig[placeholder=\"ex: https://www.instagram.com/p/CBBp48HA8u7/\"]", url);44 await page.PressAsync(".ig[placeholder=\"ex: https://www.instagram.com/p/CBBp48HA8u7/\"]", "Enter");45 await page.WaitForRequestFinishedAsync();46 // get image src47 var downloads = await page.QuerySelectorAllAsync("a[class=download-button]");48 Output.Inform($"Downloading {SetText.Blue}{SetText.Bold}{username}{SetText.ResetAll}'s Instagram post");49 for (int i = 0; i < downloads.Count; i++)50 {51 var downloadLink = downloads[i].GetAttributeAsync("href").Result.Replace("&dl=1", "");52 await FileSystem.Download(downloadLink, GetDownloadPath(folderPath, $"{username} slide {i} {new Random().Next(100, 999)}." + (downloadLink.Contains("dst-jpg") ? ".png" : ".mp4")));53 }54 //exit55 await page.CloseAsync();56 return;57 }58 //exit59 await page.CloseAsync();60 Output.Error("Action not supported");61 return;62 }63 public static async Task HandleDiscord(long userId, string folderPath) {64 // go to url65 using var playwright = await Playwright.CreateAsync();66 await using var browser = await playwright.Chromium.LaunchAsync( new BrowserTypeLaunchOptions { Headless = true });67 var page = await browser.NewPageAsync();68 await page.GotoAsync("https://discord.id", new PageGotoOptions{69 WaitUntil = WaitUntilState.NetworkIdle70 });71 // get user profile72 await page.TypeAsync("#inputid", userId.ToString());73 await page.ClickAsync("button.form-control");74 await page.ClickAsync(".frc-button");75 76 // get username77 string? username = null;78 try { username = await page.Locator("div.col-md-4:nth-child(2) > p:nth-child(3) > span:nth-child(3) > span:nth-child(1)").TextContentAsync(new LocatorTextContentOptions {79 Timeout = 300080 }); }81 catch { 82 try { username = await page.Locator("div.col-md-4.withdarker > p:nth-child(2) > span.resulth > span").TextContentAsync(new LocatorTextContentOptions {83 Timeout = 300084 }); }85 catch { username = "unknown"; } }86 // get pfp gif and png87 string? pfpSrc = null;88 try { pfpSrc = await page.Locator("img >> nth=0").GetAttributeAsync("src"); }89 catch {90 Output.Error("Failed to get User profile, keep in mind that Server IDs aren't supported");91 return;92 }93 var pfpDownloadPath = GetDownloadPath(folderPath, $"{username} pfp.png");94 Output.Inform($"Downloading {SetText.Blue}{SetText.Bold}{username}{SetText.ResetAll}'s profile picture (PNG)");95 await FileSystem.Download(pfpSrc.Replace("?size=1024", "?size=2048"), pfpDownloadPath);96 Output.Inform($"Downloading {SetText.Blue}{SetText.Bold}{username}{SetText.ResetAll}'s profile picture (GIF)");...
LocatorTextContentOptions.cs
Source: LocatorTextContentOptions.cs
...35using System.Threading.Tasks;36#nullable enable37namespace Microsoft.Playwright38{39 public class LocatorTextContentOptions40 {41 public LocatorTextContentOptions() { }42 public LocatorTextContentOptions(LocatorTextContentOptions clone)43 {44 if (clone == null)45 {46 return;47 }48 Timeout = clone.Timeout;49 }50 /// <summary>51 /// <para>52 /// Maximum time in milliseconds, defaults to 30 seconds, pass <c>0</c> to disable timeout.53 /// The default value can be changed by using the <see cref="IBrowserContext.SetDefaultTimeout"/>54 /// or <see cref="IPage.SetDefaultTimeout"/> methods.55 /// </para>56 /// </summary>...
LocatorTextContentOptions
Using AI Code Generation
1using Microsoft.Playwright;2using System;3using System.Threading.Tasks;4{5 static async Task Main(string[] args)6 {7 await using var playwright = await Playwright.CreateAsync();8 await using var browser = await playwright.Chromium.LaunchAsync();9 var page = await browser.NewPageAsync();10 await page.ClickAsync("text=English");11 await page.ClickAsync("text=Deutsch");12 await page.ClickAsync("text=Espa�ol");13 await page.ClickAsync("text=Fran�ais");14 await page.ClickAsync("text=Italiano");15 await page.ClickAsync("text=Portugu�s");16 await page.ClickAsync("text=Polski");17 await page.ClickAsync("text=�S�");18 await page.ClickAsync("text=Nederlands");
LocatorTextContentOptions
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using Microsoft.Playwright;4{5 {6 static async Task Main(string[] args)7 {8 await using var playwright = await Playwright.CreateAsync();9 await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions10 {11 });12 var page = await browser.NewPageAsync();13 var search = await page.QuerySelectorAsync("input[title='Search']");14 await search.TypeAsync("Playwright");15 await search.PressAsync("Enter");16 await page.WaitForLoadStateAsync(LoadState.Networkidle);17 var content = await page.QuerySelectorAsync("div#search div.g");18 var text = await content.EvaluateAsync<string>("element => element.textContent");19 Console.WriteLine(text);20 }21 }22}
LocatorTextContentOptions
Using AI Code Generation
1using Microsoft.Playwright;2using System;3using System.Threading.Tasks;4{5 {6 static async Task Main(string[] args)7 {8 using var playwright = await Playwright.CreateAsync();9 await using var browser = await playwright.Chromium.LaunchAsync();10 var page = await browser.NewPageAsync();11 await page.TypeAsync("input[name=q]", "Hello World!");12 await page.ClickAsync("input[type=submit]");13 await page.WaitForSelectorAsync("div#result-stats", new LocatorTextContentOptions14 {15 });16 }17 }18}19Test run for C:\Users\username\source\repos\PlaywrightTest\PlaywrightTest\bin\Debug\netcoreapp3.1\PlaywrightTest.dll(.NETCoreApp,Version=v3.1)20Microsoft (R) Test Execution Command Line Tool Version 16.7.121await page.TypeAsync("input[name=q]", "Hello World!");22Test run for C:\Users\username\source\repos\PlaywrightTest\PlaywrightTest\bin\Debug\netcoreapp3.1\PlaywrightTest.dll(.NETCore
LocatorTextContentOptions
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using Microsoft.Playwright;4using Microsoft.Playwright.Core;5using Microsoft.Playwright.Helpers;6{7 {8 static async Task Main(string[] args)9 {10 await using var playwright = await Playwright.CreateAsync();11 await using var browser = await playwright.Chromium.LaunchAsync();12 var page = await browser.NewPageAsync();13 var locator = page.Locator("input[type='text']");14 var options = new LocatorTextContentOptions();15 options.Exact = true;16 var element = await locator.WithTextAsync("Google Search", options);17 await element.ClickAsync();18 }19 }20}21at Microsoft.Playwright.Core.LocatorImpl.WithTextAsync(String text, LocatorTextContentOptions options, Boolean isRegex)22at Microsoft.Playwright.Core.LocatorImpl.WithTextAsync(String text, LocatorTextContentOptions options)23at PlaywrightTest.Program.Main(String[] args) in C:\Users\user\source\repos\PlaywrightTest\PlaywrightTest\Program.cs:line 2124Locator.WithTextAsync() method25Locator.WithTextAsync() method has the following parameters:26Locator.WithTextAsync() method is used by Locator
LocatorTextContentOptions
Using AI Code Generation
1using Microsoft.Playwright;2using System;3using System.Threading.Tasks;4{5 {6 static async Task Main(string[] args)7 {8 await using var playwright = await Playwright.CreateAsync();9 await using var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions10 {11 });12 var context = await browser.NewContextAsync();13 var page = await context.NewPageAsync();14 await page.ClickAsync("text=Sign in");15 await page.ClickAsync("input[name=\"identifier\"]");16 await page.TypeAsync("input[name=\"identifier\"]", "
Playwright Multiple Elements - Is there an equivalent to Selenium FindElements?
How to handle multiple file downloads in Playwright?
Run Playwright.NET tests in Docker container
How to handle multiple file downloads in Playwright?
Running playwright in headed mode C#
Playwright (.NET) tries to use different browser versions than installed
Playwright "Element is not attached to the DOM"
Playwright Multiple Elements - Is there an equivalent to Selenium FindElements?
Microsoft.Playwright.PlaywrightException : unable to verify the first certificate Using Playwright C# While connecting Moon
How do you create a global configuration for Playwright .NET?
Using a selector that finds a list of locators in Playwright is exactly the same as calling .FindElements() in selenium, except that Playwright does not have a specifically named method like .FindLocators().
Playwright - a selector that matches multiple elements returns a list of locators, which you then iterate over:
var rows = page.GetByRole(AriaRole.Listitem);
var count = await rows.CountAsync();
for (int i = 0; i < count; ++i)
Console.WriteLine(await rows.Nth(i).TextContentAsync());
Selenium - FindElements returns a list of elements that you have to iterate over.
IList < IWebElement > elements = driver.FindElements(By.TagName("p"));
foreach(IWebElement e in elements) {
System.Console.WriteLine(e.Text);
}
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!