How to use SelectorException class of PuppeteerSharp package

Best Puppeteer-sharp code snippet using PuppeteerSharp.SelectorException

DOMWorld.cs

Source:DOMWorld.cs Github

copy

Full Screen

...247        {248            var handle = await QuerySelectorAsync(selector).ConfigureAwait(false);249            if (handle == null)250            {251                throw new SelectorException($"No node found for selector: {selector}", selector);252            }253            await handle.ClickAsync(options).ConfigureAwait(false);254            await handle.DisposeAsync().ConfigureAwait(false);255        }256        internal async Task HoverAsync(string selector)257        {258            var handle = await QuerySelectorAsync(selector).ConfigureAwait(false);259            if (handle == null)260            {261                throw new SelectorException($"No node found for selector: {selector}", selector);262            }263            await handle.HoverAsync().ConfigureAwait(false);264            await handle.DisposeAsync().ConfigureAwait(false);265        }266        internal async Task FocusAsync(string selector)267        {268            var handle = await QuerySelectorAsync(selector).ConfigureAwait(false);269            if (handle == null)270            {271                throw new SelectorException($"No node found for selector: {selector}", selector);272            }273            await handle.FocusAsync().ConfigureAwait(false);274            await handle.DisposeAsync().ConfigureAwait(false);275        }276        internal async Task<string[]> SelectAsync(string selector, params string[] values)277        {278            if (!((await QuerySelectorAsync(selector).ConfigureAwait(false)) is ElementHandle handle))279            {280                throw new SelectorException($"No node found for selector: {selector}", selector);281            }282            var result = await handle.SelectAsync(values).ConfigureAwait(false);283            await handle.DisposeAsync();284            return result;285        }286        internal async Task TapAsync(string selector)287        {288            var handle = await QuerySelectorAsync(selector).ConfigureAwait(false);289            if (handle == null)290            {291                throw new SelectorException($"No node found for selector: {selector}", selector);292            }293            await handle.TapAsync().ConfigureAwait(false);294            await handle.DisposeAsync().ConfigureAwait(false);295        }296        internal async Task TypeAsync(string selector, string text, TypeOptions options = null)297        {298            var handle = await QuerySelectorAsync(selector).ConfigureAwait(false);299            if (handle == null)300            {301                throw new SelectorException($"No node found for selector: {selector}", selector);302            }303            await handle.TypeAsync(text, options).ConfigureAwait(false);304            await handle.DisposeAsync().ConfigureAwait(false);305        }306        internal Task<ElementHandle> WaitForSelectorAsync(string selector, WaitForSelectorOptions options = null)307            => WaitForSelectorOrXPathAsync(selector, false, options);308        internal Task<ElementHandle> WaitForXPathAsync(string xpath, WaitForSelectorOptions options = null)309            => WaitForSelectorOrXPathAsync(xpath, true, options);310        internal Task<JSHandle> WaitForFunctionAsync(string script, WaitForFunctionOptions options, params object[] args)311            => new WaitTask(312                this,313                script,314                false,315                "function",...

Full Screen

Full Screen

PuppeteerHandleExtensions.cs

Source:PuppeteerHandleExtensions.cs Github

copy

Full Screen

...13        /// <param name="elementHandleTask">A task that returns an <see cref="ElementHandle"/> that will be used as the first argument in <paramref name="pageFunction"/></param>14        /// <param name="pageFunction">Function to be evaluated in browser context</param>15        /// <param name="args">Arguments to pass to <c>pageFunction</c></param>16        /// <returns>Task</returns>17        /// <exception cref="SelectorException">If <paramref name="elementHandleTask"/> resolves to <c>null</c></exception>18        public static Task EvaluateFunctionAsync(this Task<ElementHandle> elementHandleTask, string pageFunction, params object[] args)19            => elementHandleTask.EvaluateFunctionAsync<object>(pageFunction, args);20        /// <summary>21        /// Runs <paramref name="pageFunction"/> within the frame and passes it the outcome of <paramref name="elementHandleTask"/> as the first argument22        /// </summary>23        /// <typeparam name="T">The type of the response</typeparam>24        /// <param name="elementHandleTask">A task that returns an <see cref="ElementHandle"/> that will be used as the first argument in <paramref name="pageFunction"/></param>25        /// <param name="pageFunction">Function to be evaluated in browser context</param>26        /// <param name="args">Arguments to pass to <c>pageFunction</c></param>27        /// <returns>Task which resolves to the return value of <c>pageFunction</c></returns>28        /// <exception cref="SelectorException">If <paramref name="elementHandleTask"/> resolves to <c>null</c></exception>29        public static async Task<T> EvaluateFunctionAsync<T>(this Task<ElementHandle> elementHandleTask, string pageFunction, params object[] args)30        {31            if (elementHandleTask == null)32            {33                throw new ArgumentNullException(nameof(elementHandleTask));34            }35            var elementHandle = await elementHandleTask.ConfigureAwait(false);36            if (elementHandle == null)37            {38                throw new SelectorException("Error: failed to find element matching selector");39            }40            return await elementHandle.EvaluateFunctionAsync<T>(pageFunction, args).ConfigureAwait(false);41        }42        /// <summary>43        /// Runs <paramref name="pageFunction"/> within the frame and passes it the outcome the <paramref name="elementHandle"/> as the first argument44        /// </summary>45        /// <typeparam name="T">The type of the response</typeparam>46        /// <param name="elementHandle">An <see cref="ElementHandle"/> that will be used as the first argument in <paramref name="pageFunction"/></param>47        /// <param name="pageFunction">Function to be evaluated in browser context</param>48        /// <param name="args">Arguments to pass to <c>pageFunction</c></param>49        /// <returns>Task which resolves to the return value of <c>pageFunction</c></returns>50        /// <exception cref="SelectorException">If <paramref name="elementHandle"/> is <c>null</c></exception>51        public static async Task<T> EvaluateFunctionAsync<T>(this ElementHandle elementHandle, string pageFunction, params object[] args)52        {53            if (elementHandle == null)54            {55                throw new SelectorException("Error: failed to find element matching selector");56            }57            var result = await elementHandle.EvaluateFunctionAsync<T>(pageFunction, args).ConfigureAwait(false);58            await elementHandle.DisposeAsync().ConfigureAwait(false);59            return result;60        }61        /// <summary>62        /// Runs <paramref name="pageFunction"/> within the frame and passes it the outcome of <paramref name="arrayHandleTask"/> as the first argument. Use only after <see cref="Page.QuerySelectorAllHandleAsync(string)"/>63        /// </summary>64        /// <param name="arrayHandleTask">A task that returns an <see cref="JSHandle"/> that represents an array of <see cref="ElementHandle"/> that will be used as the first argument in <paramref name="pageFunction"/></param>65        /// <param name="pageFunction">Function to be evaluated in browser context</param>66        /// <param name="args">Arguments to pass to <c>pageFunction</c></param>67        /// <returns>Task</returns>68        public static Task EvaluateFunctionAsync(this Task<JSHandle> arrayHandleTask, string pageFunction, params object[] args)69            => arrayHandleTask.EvaluateFunctionAsync<object>(pageFunction, args);...

Full Screen

Full Screen

Extensions.cs

Source:Extensions.cs Github

copy

Full Screen

...12        /// <param name="elementHandleTask">A task that returns an <see cref="ElementHandle"/> that will be used as the first argument in <paramref name="pageFunction"/></param>13        /// <param name="pageFunction">Function to be evaluated in browser context</param>14        /// <param name="args">Arguments to pass to <c>pageFunction</c></param>15        /// <returns>Task</returns>16        /// <exception cref="SelectorException">If <paramref name="elementHandleTask"/> resolves to <c>null</c></exception>17        public static Task EvaluateFunctionAsync(this Task<ElementHandle> elementHandleTask, string pageFunction, params object[] args)18            => elementHandleTask.EvaluateFunctionAsync<object>(pageFunction, args);19        /// <summary>20        /// Runs <paramref name="pageFunction"/> within the frame and passes it the outcome of <paramref name="elementHandleTask"/> as the first argument21        /// </summary>22        /// <typeparam name="T">The type of the response</typeparam>23        /// <param name="elementHandleTask">A task that returns an <see cref="ElementHandle"/> that will be used as the first argument in <paramref name="pageFunction"/></param>24        /// <param name="pageFunction">Function to be evaluated in browser context</param>25        /// <param name="args">Arguments to pass to <c>pageFunction</c></param>26        /// <returns>Task which resolves to the return value of <c>pageFunction</c></returns>27        /// <exception cref="SelectorException">If <paramref name="elementHandleTask"/> resolves to <c>null</c></exception>28        public static async Task<T> EvaluateFunctionAsync<T>(this Task<ElementHandle> elementHandleTask, string pageFunction, params object[] args)29        {30            var elementHandle = await elementHandleTask.ConfigureAwait(false);31            if (elementHandle == null)32            {33                throw new SelectorException("Error: failed to find element matching selector");34            }35            return await elementHandle.EvaluateFunctionAsync<T>(pageFunction, args).ConfigureAwait(false);36        }37        /// <summary>38        /// Runs <paramref name="pageFunction"/> within the frame and passes it the outcome the <paramref name="elementHandle"/> as the first argument39        /// </summary>40        /// <typeparam name="T">The type of the response</typeparam>41        /// <param name="elementHandle">An <see cref="ElementHandle"/> that will be used as the first argument in <paramref name="pageFunction"/></param>42        /// <param name="pageFunction">Function to be evaluated in browser context</param>43        /// <param name="args">Arguments to pass to <c>pageFunction</c></param>44        /// <returns>Task which resolves to the return value of <c>pageFunction</c></returns>45        /// <exception cref="SelectorException">If <paramref name="elementHandle"/> is <c>null</c></exception>46        public static async Task<T> EvaluateFunctionAsync<T>(this ElementHandle elementHandle, string pageFunction, params object[] args)47        {48            if (elementHandle == null)49            {50                throw new SelectorException("Error: failed to find element matching selector");51            }52            var result = await elementHandle.EvaluateFunctionAsync<T>(pageFunction, args).ConfigureAwait(false);53            await elementHandle.DisposeAsync().ConfigureAwait(false);54            return result;55        }56        /// <summary>57        /// Runs <paramref name="pageFunction"/> within the frame and passes it the outcome of <paramref name="arrayHandleTask"/> as the first argument. Use only after <see cref="Page.QuerySelectorAllHandleAsync(string)"/>58        /// </summary>59        /// <param name="arrayHandleTask">A task that returns an <see cref="JSHandle"/> that represents an array of <see cref="ElementHandle"/> that will be used as the first argument in <paramref name="pageFunction"/></param>60        /// <param name="pageFunction">Function to be evaluated in browser context</param>61        /// <param name="args">Arguments to pass to <c>pageFunction</c></param>62        /// <returns>Task</returns>63        public static Task EvaluateFunctionAsync(this Task<JSHandle> arrayHandleTask, string pageFunction, params object[] args)64            => arrayHandleTask.EvaluateFunctionAsync<object>(pageFunction, args);...

Full Screen

Full Screen

BrowserActor.cs

Source:BrowserActor.cs Github

copy

Full Screen

...102                        else if (exception is NavigationException) // Couldn't Navigate to url. Or Browser was disconnected //Target.detachedFromTarget103                        {104                            return Directive.Restart;105                        }106                        else if (exception is SelectorException)107                        {108                        }109                        else if (exception is TargetClosedException) // Page was closed110                        {111                            return Directive.Restart;112                        }113                    }114                    else if (exception is NullReferenceException)115                    {116                        return Directive.Escalate;117                    }118                    return Directive.Resume;119                });120        }...

Full Screen

Full Screen

ElementHandleQuerySelectorEvalTests.cs

Source:ElementHandleQuerySelectorEvalTests.cs Github

copy

Full Screen

...48        {49            var htmlContent = "<div class=\"a\">not-a-child-div</div><div id=\"myId\"></div>";50            await Page.SetContentAsync(htmlContent);51            var elementHandle = await Page.QuerySelectorAsync("#myId");52            var exception = await Assert.ThrowsAsync<SelectorException>(53                () => elementHandle.QuerySelectorAsync(".a").EvaluateFunctionAsync<string>("node => node.innerText")54            );55            Assert.Equal("Error: failed to find element matching selector", exception.Message);56        }57    }58}...

Full Screen

Full Screen

PageQuerySelectorEvalTests.cs

Source:PageQuerySelectorEvalTests.cs Github

copy

Full Screen

...47        [PuppeteerTest("queryselector.spec.ts", "Page.$eval", "should throw error if no element is found")]48        [PuppeteerFact]49        public async Task ShouldThrowErrorIfNoElementIsFound()50        {51            var exception = await Assert.ThrowsAsync<SelectorException>(()52                => Page.QuerySelectorAsync("section").EvaluateFunctionAsync<string>("e => e.id"));53            Assert.Contains("failed to find element matching selector", exception.Message);54        }55    }56}...

Full Screen

Full Screen

SelectorException.cs

Source:SelectorException.cs Github

copy

Full Screen

...12    /// <seealso cref="Page.HoverAsync(string)"/>13    /// <seealso cref="Page.FocusAsync(string)"/>14    /// <seealso cref="Page.SelectAsync(string, string[])"/>15    [Serializable]16    public class SelectorException : PuppeteerException17    {18        /// <summary>19        /// Gets the selector.20        /// </summary>21        /// <value>The selector.</value>22        public string Selector { get; }23        /// <summary>24        /// Initializes a new instance of the <see cref="SelectorException"/> class.25        /// </summary>26        /// <param name="message">Message.</param>27        public SelectorException(string message) : base(message)28        {29        }30        /// <summary>31        /// Initializes a new instance of the <see cref="SelectorException"/> class.32        /// </summary>33        /// <param name="message">Message.</param>34        /// <param name="selector">Selector.</param>35        public SelectorException(string message, string selector) : base(message)36        {37            Selector = selector;38        }39        /// <summary>40        /// Initializes a new instance of the <see cref="SelectorException"/> class.41        /// </summary>42        /// <param name="info">Serialization Info.</param>43        /// <param name="context">Streaming Context.</param>44        protected SelectorException(SerializationInfo info, StreamingContext context) : base(info, context)45        {46        }47    }48}...

Full Screen

Full Screen

EvalTests.cs

Source:EvalTests.cs Github

copy

Full Screen

...33        }34        [Fact]35        public async Task ShouldThrowErrorIfNoElementIsFound()36        {37            var exception = await Assert.ThrowsAsync<SelectorException>(()38                => Page.QuerySelectorAsync("section").EvaluateFunctionAsync<string>("e => e.id"));39            Assert.Contains("failed to find element matching selector", exception.Message);40        }41    }42}...

Full Screen

Full Screen

SelectorException

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp;2using System;3using System.Threading.Tasks;4{5    {6        static async Task Main(string[] args)7        {8            await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);9            await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);10            var browser = await Puppeteer.LaunchAsync(new LaunchOptions11            {12            });13            var page = await browser.NewPageAsync();14            await page.GoToAsync("

Full Screen

Full Screen

SelectorException

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp;2using System;3using System.Threading.Tasks;4{5    {6        static async Task Main(string[] args)7        {8            var browser = await Puppeteer.LaunchAsync(new LaunchOptions9            {10            });11            var page = await browser.NewPageAsync();12            await page.WaitForSelectorAsync("input[name='q']");13            await page.TypeAsync("input[name='q']", "PuppeteerSharp");14            await page.ClickAsync("input[name='btnK']");15            await page.WaitForSelectorAsync("h3.LC20lb");16            await page.ClickAsync("h3.LC20lb");17            {18                await page.WaitForSelectorAsync("h3.LC20lb", new WaitForSelectorOptions { Timeout = 1000 });19            }20            catch (SelectorException e)21            {22                Console.WriteLine(e.Message);23            }24            await browser.CloseAsync();25        }26    }27}

Full Screen

Full Screen

SelectorException

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp;2using System;3using System.Threading.Tasks;4{5    {6        static async Task Main(string[] args)7        {8            var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });9            var page = await browser.NewPageAsync();10            {11                await page.ClickAsync("button");12            }13            catch (SelectorException e)14            {15                Console.WriteLine(e.Message);16            }17            catch (Exception e)18            {19                Console.WriteLine(e.Message);20            }21            Console.ReadKey();22        }23    }24}25   at PuppeteerSharp.Page.ClickAsync(String selector, ClickOptions options)26   at PuppeteerSharpDemo.Program.Main(String[] args) in C:\Users\username\source\repos\PuppeteerSharpDemo\PuppeteerSharpDemo\Program.cs:line 15

Full Screen

Full Screen

SelectorException

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp;2{3    {4        public SelectorException(string message) : base(message)5        {6        }7    }8}9using PuppeteerSharp;10{11    {12        public SelectorException(string message) : base(message)13        {14        }15    }16}17using PuppeteerSharp;18{19    {20        public SelectorException(string message) : base(message)21        {22        }23    }24}25using PuppeteerSharp;26{27    {28        public SelectorException(string message) : base(message)29        {30        }31    }32}33using PuppeteerSharp;34{35    {36        public SelectorException(string message) : base(message)37        {38        }39    }40}41using PuppeteerSharp;42{43    {44        public SelectorException(string message) : base(message)45        {46        }47    }48}49using PuppeteerSharp;50{51    {52        public SelectorException(string message) : base(message)53        {54        }55    }56}57using PuppeteerSharp;58{59    {60        public SelectorException(string message) : base(message)61        {62        }63    }64}65using PuppeteerSharp;

Full Screen

Full Screen

SelectorException

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp;2{3    {4        static void Main(string[] args)5        {6            MainAsync().Wait();7        }8        static async Task MainAsync()9        {10            {11            };12            using (var browser = await Puppeteer.LaunchAsync(options))13            using (var page = await browser.NewPageAsync())14            {15                {16                    await page.WaitForSelectorAsync("input[name='q']");17                }18                catch (SelectorException)19                {20                    Console.WriteLine("SelectorException");21                }22            }23        }24    }25}

Full Screen

Full Screen

SelectorException

Using AI Code Generation

copy

Full Screen

1var selector = "a";2var elementHandle = await page.QuerySelectorAsync(selector);3if (elementHandle == null)4{5    throw new SelectorException($"failed to find element matching selector {selector}");6}7var text = await elementHandle.EvaluateFunctionAsync<string>("node => node.textContent");8Console.WriteLine(text);9var selector = "a";10var elementHandle = await page.QuerySelectorAsync(selector);11if (elementHandle == null)12{13    throw new WaitTaskException($"failed to find element matching selector {selector}");14}15var text = await elementHandle.EvaluateFunctionAsync<string>("node => node.textContent");16Console.WriteLine(text);17var selector = "a";18var elementHandle = await page.QuerySelectorAsync(selector);19if (elementHandle == null)20{21    throw new PuppeteerException($"failed to find element matching selector {selector}");22}23var text = await elementHandle.EvaluateFunctionAsync<string>("node => node.textContent");24Console.WriteLine(text);25var selector = "a";26var elementHandle = await page.QuerySelectorAsync(selector);27if (elementHandle == null)28{29    throw new Exception($"failed to find element matching selector {selector}");30}31var text = await elementHandle.EvaluateFunctionAsync<string>("node => node.textContent");32Console.WriteLine(text);33var selector = "a";34var elementHandle = await page.QuerySelectorAsync(selector);35if (elementHandle == null)36{37    throw new Exception($"failed to find element matching selector {selector}");38}39var text = await elementHandle.EvaluateFunctionAsync<string>("node => node.textContent");40Console.WriteLine(text);41var selector = "a";42var elementHandle = await page.QuerySelectorAsync(selector);43if (elementHandle == null)44{45    throw new PuppeteerException($"failed to find element matching selector {selector}");46}47var text = await elementHandle.EvaluateFunctionAsync<string>("node => node.textContent");48Console.WriteLine(text);49var selector = "a";

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in SelectorException

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful