Best Puppeteer-sharp code snippet using PuppeteerSharp.Frame.NavigatedWithinDocument
Frame.cs
Source:Frame.cs
...387 Name = framePayload.Name ?? string.Empty;388 NavigationURL = framePayload.Url;389 Url = framePayload.Url;390 }391 internal void NavigatedWithinDocument(string url) => Url = url;392 internal void Detach()393 {394 Detached = true;395 MainWorld.Detach();396 SecondaryWorld.Detach();397 if (ParentFrame != null)398 {399 ParentFrame.ChildFrames.Remove(this);400 }401 ParentFrame = null;402 }403 #endregion404 }405}...
FrameManager.cs
Source:FrameManager.cs
...28 #region Properties29 internal event EventHandler<FrameEventArgs> FrameAttached;30 internal event EventHandler<FrameEventArgs> FrameDetached;31 internal event EventHandler<FrameEventArgs> FrameNavigated;32 internal event EventHandler<FrameEventArgs> FrameNavigatedWithinDocument;33 internal event EventHandler<FrameEventArgs> LifecycleEvent;34 internal Dictionary<string, Frame> Frames { get; set; }35 internal Frame MainFrame { get; set; }36 internal Page Page { get; }37 internal int DefaultNavigationTimeout { get; set; } = 30000;38 #endregion39 #region Public Methods40 internal ExecutionContext ExecutionContextById(int contextId)41 {42 _contextIdToContext.TryGetValue(contextId, out var context);43 if (context == null)44 {45 _logger.LogError("INTERNAL ERROR: missing context with id = {ContextId}", contextId);46 }47 return context;48 }49 public async Task<Response> NavigateFrameAsync(Frame frame, string url, NavigationOptions options)50 {51 var referrer = string.IsNullOrEmpty(options.Referer)52 ? _networkManager.ExtraHTTPHeaders?.GetValueOrDefault(MessageKeys.Referer)53 : options.Referer;54 var requests = new Dictionary<string, Request>();55 var timeout = options?.Timeout ?? DefaultNavigationTimeout;56 var watcher = new NavigatorWatcher(_client, this, frame, _networkManager, timeout, options);57 var navigateTask = NavigateAsync(_client, url, referrer, frame.Id);58 await Task.WhenAny(59 watcher.TimeoutOrTerminationTask,60 navigateTask).ConfigureAwait(false);61 AggregateException exception = null;62 if (navigateTask.IsFaulted)63 {64 exception = navigateTask.Exception;65 }66 else67 {68 await Task.WhenAny(69 watcher.TimeoutOrTerminationTask,70 _ensureNewDocumentNavigation ? watcher.NewDocumentNavigationTask : watcher.SameDocumentNavigationTask71 ).ConfigureAwait(false);72 if (watcher.TimeoutOrTerminationTask.IsCompleted && watcher.TimeoutOrTerminationTask.Result.IsFaulted)73 {74 exception = watcher.TimeoutOrTerminationTask.Result.Exception;75 }76 }77 if (exception != null)78 {79 throw new NavigationException(exception.InnerException.Message, exception.InnerException);80 }81 return watcher.NavigationResponse;82 }83 private async Task NavigateAsync(CDPSession client, string url, string referrer, string frameId)84 {85 var response = await client.SendAsync<PageNavigateResponse>("Page.navigate", new86 {87 url,88 referrer = referrer ?? string.Empty,89 frameId90 }).ConfigureAwait(false);91 _ensureNewDocumentNavigation = !string.IsNullOrEmpty(response.LoaderId);92 if (!string.IsNullOrEmpty(response.ErrorText))93 {94 throw new NavigationException(response.ErrorText, url);95 }96 }97 public async Task<Response> WaitForFrameNavigationAsync(Frame frame, NavigationOptions options = null)98 {99 var timeout = options?.Timeout ?? DefaultNavigationTimeout;100 var watcher = new NavigatorWatcher(_client, this, frame, _networkManager, timeout, options);101 var raceTask = await Task.WhenAny(102 watcher.NewDocumentNavigationTask,103 watcher.SameDocumentNavigationTask,104 watcher.TimeoutOrTerminationTask105 ).ConfigureAwait(false);106 var exception = raceTask.Exception;107 if (exception == null &&108 watcher.TimeoutOrTerminationTask.IsCompleted &&109 watcher.TimeoutOrTerminationTask.Result.IsFaulted)110 {111 exception = watcher.TimeoutOrTerminationTask.Result.Exception;112 }113 if (exception != null)114 {115 throw new NavigationException(exception.Message, exception);116 }117 return watcher.NavigationResponse;118 }119 #endregion120 #region Private Methods121 private void _client_MessageReceived(object sender, MessageEventArgs e)122 {123 switch (e.MessageID)124 {125 case "Page.frameAttached":126 OnFrameAttached(127 e.MessageData.SelectToken(MessageKeys.FrameId).ToObject<string>(),128 e.MessageData.SelectToken("parentFrameId").ToObject<string>());129 break;130 case "Page.frameNavigated":131 OnFrameNavigated(e.MessageData.SelectToken(MessageKeys.Frame).ToObject<FramePayload>());132 break;133 case "Page.navigatedWithinDocument":134 OnFrameNavigatedWithinDocument(e.MessageData.ToObject<NavigatedWithinDocumentResponse>());135 break;136 case "Page.frameDetached":137 OnFrameDetached(e.MessageData.ToObject<BasicFrameResponse>());138 break;139 case "Page.frameStoppedLoading":140 OnFrameStoppedLoading(e.MessageData.ToObject<BasicFrameResponse>());141 break;142 case "Runtime.executionContextCreated":143 OnExecutionContextCreated(e.MessageData.SelectToken(MessageKeys.Context).ToObject<ContextPayload>());144 break;145 case "Runtime.executionContextDestroyed":146 OnExecutionContextDestroyed(e.MessageData.SelectToken(MessageKeys.ExecutionContextId).ToObject<int>());147 break;148 case "Runtime.executionContextsCleared":149 OnExecutionContextsCleared();150 break;151 case "Page.lifecycleEvent":152 OnLifeCycleEvent(e.MessageData.ToObject<LifecycleEventResponse>());153 break;154 default:155 break;156 }157 }158 private void OnFrameStoppedLoading(BasicFrameResponse e)159 {160 if (Frames.TryGetValue(e.FrameId, out var frame))161 {162 frame.OnLoadingStopped();163 LifecycleEvent?.Invoke(this, new FrameEventArgs(frame));164 }165 }166 private void OnLifeCycleEvent(LifecycleEventResponse e)167 {168 if (Frames.TryGetValue(e.FrameId, out var frame))169 {170 frame.OnLifecycleEvent(e.LoaderId, e.Name);171 LifecycleEvent?.Invoke(this, new FrameEventArgs(frame));172 }173 }174 private void OnExecutionContextsCleared()175 {176 foreach (var context in _contextIdToContext.Values)177 {178 RemoveContext(context);179 }180 _contextIdToContext.Clear();181 }182 private void OnExecutionContextDestroyed(int executionContextId)183 {184 _contextIdToContext.TryGetValue(executionContextId, out var context);185 if (context != null)186 {187 _contextIdToContext.Remove(executionContextId);188 RemoveContext(context);189 }190 }191 private void OnExecutionContextCreated(ContextPayload contextPayload)192 {193 var frameId = contextPayload.AuxData.IsDefault ? contextPayload.AuxData.FrameId : null;194 var frame = !string.IsNullOrEmpty(frameId) ? Frames[frameId] : null;195 var context = new ExecutionContext(196 _client,197 contextPayload,198 frame);199 _contextIdToContext[contextPayload.Id] = context;200 if (frame != null)201 {202 frame.SetDefaultContext(context);203 }204 }205 private void OnFrameDetached(BasicFrameResponse e)206 {207 if (Frames.TryGetValue(e.FrameId, out var frame))208 {209 RemoveFramesRecursively(frame);210 }211 }212 private void OnFrameNavigated(FramePayload framePayload)213 {214 var isMainFrame = string.IsNullOrEmpty(framePayload.ParentId);215 var frame = isMainFrame ? MainFrame : Frames[framePayload.Id];216 Contract.Assert(isMainFrame || frame != null, "We either navigate top level or have old version of the navigated frame");217 // Detach all child frames first.218 if (frame != null)219 {220 while (frame.ChildFrames.Count > 0)221 {222 RemoveFramesRecursively(frame.ChildFrames[0]);223 }224 }225 // Update or create main frame.226 if (isMainFrame)227 {228 if (frame != null)229 {230 // Update frame id to retain frame identity on cross-process navigation.231 if (frame.Id != null)232 {233 Frames.Remove(frame.Id);234 }235 frame.Id = framePayload.Id;236 }237 else238 {239 // Initial main frame navigation.240 frame = new Frame(this, _client, null, framePayload.Id);241 }242 Frames[framePayload.Id] = frame;243 MainFrame = frame;244 }245 // Update frame payload.246 frame.Navigated(framePayload);247 FrameNavigated?.Invoke(this, new FrameEventArgs(frame));248 }249 private void OnFrameNavigatedWithinDocument(NavigatedWithinDocumentResponse e)250 {251 if (Frames.TryGetValue(e.FrameId, out var frame))252 {253 frame.NavigatedWithinDocument(e.Url);254 var eventArgs = new FrameEventArgs(frame);255 FrameNavigatedWithinDocument?.Invoke(this, eventArgs);256 FrameNavigated?.Invoke(this, eventArgs);257 }258 }259 private void RemoveContext(ExecutionContext context)260 {261 if (context.Frame != null)262 {263 context.Frame.SetDefaultContext(null);264 }265 }266 private void RemoveFramesRecursively(Frame frame)267 {268 while (frame.ChildFrames.Count > 0)269 {...
LifecycleWatcher.cs
Source:LifecycleWatcher.cs
...49 _newDocumentNavigationTaskWrapper = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);50 _lifecycleTaskWrapper = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);51 _terminationTaskWrapper = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);52 frameManager.LifecycleEvent += FrameManager_LifecycleEvent;53 frameManager.FrameNavigatedWithinDocument += NavigatedWithinDocument;54 frameManager.FrameDetached += OnFrameDetached;55 frameManager.NetworkManager.Request += OnRequest;56 frameManager.Client.Disconnected += OnClientDisconnected;57 CheckLifecycleComplete();58 }59 #region Properties60 public Task<Task> NavigationTask { get; internal set; }61 public Task<bool> SameDocumentNavigationTask => _sameDocumentNavigationTaskWrapper.Task;62 public Task<bool> NewDocumentNavigationTask => _newDocumentNavigationTaskWrapper.Task;63 public Response NavigationResponse => _navigationRequest?.Response;64 public Task TimeoutOrTerminationTask65 => _terminationTaskWrapper.Task.WithTimeout(_timeout);66 public Task LifecycleTask => _lifecycleTaskWrapper.Task;67 #endregion68 #region Private methods69 private void OnClientDisconnected(object sender, EventArgs e)70 => Terminate(new TargetClosedException("Navigation failed because browser has disconnected!", _frameManager.Client.CloseReason));71 void FrameManager_LifecycleEvent(object sender, FrameEventArgs e) => CheckLifecycleComplete();7273 private void OnFrameDetached(object sender, FrameEventArgs e)74 {75 var frame = e.Frame;76 if (_frame == frame)77 {78 Terminate(new PuppeteerException("Navigating frame was detached"));79 return;80 }81 CheckLifecycleComplete();82 }83 private void CheckLifecycleComplete()84 {85 // We expect navigation to commit.86 if (!CheckLifecycle(_frame, _expectedLifecycle))87 {88 return;89 }90 _lifecycleTaskWrapper.TrySetResult(true);91 if (_frame.LoaderId == _initialLoaderId && !_hasSameDocumentNavigation)92 {93 return;94 }95 if (_hasSameDocumentNavigation)96 {97 _sameDocumentNavigationTaskWrapper.TrySetResult(true);98 }99 if (_frame.LoaderId != _initialLoaderId)100 {101 _newDocumentNavigationTaskWrapper.TrySetResult(true);102 }103 }104 private void Terminate(PuppeteerException ex) => _terminationTaskWrapper.TrySetException(ex);105 private void OnRequest(object sender, RequestEventArgs e)106 {107 if (e.Request.Frame != _frame || !e.Request.IsNavigationRequest)108 {109 return;110 }111 _navigationRequest = e.Request;112 }113 private void NavigatedWithinDocument(object sender, FrameEventArgs e)114 {115 if (e.Frame != _frame)116 {117 return;118 }119 _hasSameDocumentNavigation = true;120 CheckLifecycleComplete();121 }122 private bool CheckLifecycle(Frame frame, IEnumerable<string> expectedLifecycle)123 {124 foreach (var item in expectedLifecycle)125 {126 if (!frame.LifecycleEvents.Contains(item))127 {128 return false;129 }130 }131 foreach (var child in frame.ChildFrames)132 {133 if (!CheckLifecycle(child, expectedLifecycle))134 {135 return false;136 }137 }138 return true;139 }140 public void Dispose() => Dispose(true);141 ~LifecycleWatcher() => Dispose(false);142 public void Dispose(bool disposing)143 {144 _frameManager.LifecycleEvent -= FrameManager_LifecycleEvent;145 _frameManager.FrameNavigatedWithinDocument -= NavigatedWithinDocument;146 _frameManager.FrameDetached -= OnFrameDetached;147 _frameManager.NetworkManager.Request -= OnRequest;148 _frameManager.Client.Disconnected -= OnClientDisconnected;149 }150 #endregion151 }152}...
NavigatorWatcher.cs
Source:NavigatorWatcher.cs
...52 _initialLoaderId = mainFrame.LoaderId;53 _timeout = timeout;54 _hasSameDocumentNavigation = false;55 frameManager.LifecycleEvent += CheckLifecycleComplete;56 frameManager.FrameNavigatedWithinDocument += NavigatedWithinDocument;57 frameManager.FrameDetached += OnFrameDetached;58 networkManager.Request += OnRequest;59 Connection.FromSession(client).Closed += (sender, e)60 => Terminate(new TargetClosedException("Navigation failed because browser has disconnected!"));61 _sameDocumentNavigationTaskWrapper = new TaskCompletionSource<bool>();62 _newDocumentNavigationTaskWrapper = new TaskCompletionSource<bool>();63 _terminationTaskWrapper = new TaskCompletionSource<bool>();64 _timeoutTask = TaskHelper.CreateTimeoutTask(timeout);65 }66 #region Properties67 public Task<Task> NavigationTask { get; internal set; }68 public Task<bool> SameDocumentNavigationTask => _sameDocumentNavigationTaskWrapper.Task;69 public Task<bool> NewDocumentNavigationTask => _newDocumentNavigationTaskWrapper.Task;70 public Response NavigationResponse => _navigationRequest?.Response;71 public Task<Task> TimeoutOrTerminationTask => Task.WhenAny(_timeoutTask, _terminationTaskWrapper.Task);72 #endregion73 #region Private methods74 private void OnFrameDetached(object sender, FrameEventArgs e)75 {76 var frame = e.Frame;77 if (_frame == frame)78 {79 Terminate(new PuppeteerException("Navigating frame was detached"));80 return;81 }82 CheckLifecycleComplete(sender, e);83 }84 private void CheckLifecycleComplete(object sender, FrameEventArgs e)85 {86 // We expect navigation to commit.87 if (_frame.LoaderId == _initialLoaderId && !_hasSameDocumentNavigation)88 {89 return;90 }91 if (!CheckLifecycle(_frame, _expectedLifecycle))92 {93 return;94 }95 if (_hasSameDocumentNavigation)96 {97 _sameDocumentNavigationTaskWrapper.TrySetResult(true);98 }99 if (_frame.LoaderId != _initialLoaderId)100 {101 _newDocumentNavigationTaskWrapper.TrySetResult(true);102 }103 }104 private void Terminate(PuppeteerException ex) => _terminationTaskWrapper.TrySetException(ex);105 private void OnRequest(object sender, RequestEventArgs e)106 {107 if (e.Request.Frame != _frame || !e.Request.IsNavigationRequest)108 {109 return;110 }111 _navigationRequest = e.Request;112 }113 private void NavigatedWithinDocument(object sender, FrameEventArgs e)114 {115 if (e.Frame != _frame)116 {117 return;118 }119 _hasSameDocumentNavigation = true;120 CheckLifecycleComplete(sender, e);121 }122 private bool CheckLifecycle(Frame frame, IEnumerable<string> expectedLifecycle)123 {124 foreach (var item in expectedLifecycle)125 {126 if (!frame.LifecycleEvents.Contains(item))127 {...
NavigatedWithinDocument
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);9 var browser = await Puppeteer.LaunchAsync(new LaunchOptions10 {11 Args = new string[] { "--start-maximized" }12 });13 var page = await browser.NewPageAsync();14 await page.WaitForSelectorAsync("input[name=q]");15 await page.TypeAsync("input[name=q]", "PuppeteerSharp");16 await page.ClickAsync("input[value='Google Search']");17 await page.WaitForNavigationAsync();18 await page.WaitForSelectorAsync("a[href='
NavigatedWithinDocument
Using AI Code Generation
1using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions2{3}))4{5 var page = await browser.NewPageAsync();6 await page.TypeAsync("input[title='Search']", "PuppeteerSharp");7 await page.ClickAsync("input[value='Google Search']");8 await page.WaitForNavigationAsync();9 await page.ClickAsync("a[href='
NavigatedWithinDocument
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 var options = new LaunchOptions { Headless = false };9 using (var browser = await Puppeteer.LaunchAsync(options))10 using (var page = await browser.NewPageAsync())11 {12 await page.WaitForSelectorAsync("input[name='q']");13 await page.TypeAsync("input[name='q']", "PuppeteerSharp", new TypeOptions { Delay = 100 });14 await page.ClickAsync("input[name='btnK']");15 await page.WaitForNavigationAsync();16 await page.WaitForSelectorAsync("a[href='
NavigatedWithinDocument
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 public static async Task Main(string[] args)7 {8 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);9 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false }))10 using (var page = await browser.NewPageAsync())11 {12 await page.EvaluateFunctionAsync("() => { document.getElementById('lst-ib').value = 'Hello World!'; }");13 await page.NavigatedWithinDocumentAsync();14 await page.WaitForSelectorAsync("#resultStats");15 await page.ScreenshotAsync("google.png");16 }17 }18 }19}
NavigatedWithinDocument
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);9 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });10 var page = await browser.NewPageAsync();11 await page.TypeAsync("input[title='Search']", "Puppeteer");12 await page.ClickAsync("input[value='Google Search']");13 await page.WaitForNavigationAsync();14 await page.WaitForNavigationAsync();15 await page.NavigatedWithinDocumentAsync();16 Console.ReadKey();17 }18 }19}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!