@@ -42,6 +42,7 @@ public class Page : IDisposable
4242 private readonly Dictionary < string , Delegate > _pageBindings ;
4343 private readonly Dictionary < string , Worker > _workers ;
4444 private readonly ILogger _logger ;
45+ private bool _ensureNewDocumentNavigation ;
4546
4647 private static readonly Dictionary < string , decimal > _unitToPixels = new Dictionary < string , decimal > {
4748 { "px" , 1 } ,
@@ -716,7 +717,7 @@ void createRequestEventListener(object sender, RequestEventArgs e)
716717 var navigateTask = Navigate ( Client , url , referrer ) ;
717718
718719 await Task . WhenAny (
719- watcher . NavigationTask ,
720+ watcher . TimeoutTask ,
720721 navigateTask ) . ConfigureAwait ( false ) ;
721722
722723 AggregateException exception = null ;
@@ -725,21 +726,19 @@ await Task.WhenAny(
725726 {
726727 exception = navigateTask . Exception ;
727728 }
728- else if ( watcher . NavigationTask . IsCompleted &&
729- watcher . NavigationTask . Result . IsFaulted )
729+ else
730730 {
731- exception = watcher . NavigationTask . Result ? . Exception ;
732- }
731+ await Task . WhenAny (
732+ watcher . TimeoutTask ,
733+ _ensureNewDocumentNavigation ? watcher . NewDocumentNavigationTask : watcher . SameDocumentNavigationTask
734+ ) . ConfigureAwait ( false ) ;
733735
734- if ( exception == null )
735- {
736- await Task . WhenAll (
737- watcher . NavigationTask ,
738- navigateTask ) . ConfigureAwait ( false ) ;
739- exception = navigateTask . Exception ?? watcher . NavigationTask . Result . Exception ;
736+ if ( watcher . TimeoutTask . IsFaulted )
737+ {
738+ exception = watcher . TimeoutTask . Exception ;
739+ }
740740 }
741741
742- watcher . Cancel ( ) ;
743742 _networkManager . Request -= createRequestEventListener ;
744743
745744 if ( exception != null )
@@ -1408,11 +1407,15 @@ public async Task<Response> WaitForNavigationAsync(NavigationOptions options = n
14081407
14091408 _networkManager . Response += createResponseEventListener ;
14101409
1411- await watcher . NavigationTask . ConfigureAwait ( false ) ;
1410+ var raceTask = await Task . WhenAny (
1411+ watcher . NewDocumentNavigationTask ,
1412+ watcher . SameDocumentNavigationTask ,
1413+ watcher . TimeoutTask
1414+ ) . ConfigureAwait ( false ) ;
14121415
14131416 _networkManager . Response -= createResponseEventListener ;
14141417
1415- var exception = watcher . NavigationTask . Exception ;
1418+ var exception = raceTask . Exception ;
14161419 if ( exception != null )
14171420 {
14181421 throw new NavigationException ( exception . Message , exception ) ;
@@ -2025,15 +2028,17 @@ await Task.WhenAll(Frames.Select(frame => frame.EvaluateExpressionAsync(expressi
20252028
20262029 private async Task Navigate ( CDPSession client , string url , string referrer )
20272030 {
2028- dynamic response = await client . SendAsync ( "Page.navigate" , new
2031+ var response = await client . SendAsync < PageNavigateResponse > ( "Page.navigate" , new
20292032 {
20302033 url ,
20312034 referrer = referrer ?? string . Empty
20322035 } ) . ConfigureAwait ( false ) ;
20332036
2034- if ( response . errorText != null )
2037+ _ensureNewDocumentNavigation = ! string . IsNullOrEmpty ( response . LoaderId ) ;
2038+
2039+ if ( ! string . IsNullOrEmpty ( response . ErrorText ) )
20352040 {
2036- throw new NavigationException ( response . errorText . ToString ( ) , url ) ;
2041+ throw new NavigationException ( response . ErrorText , url ) ;
20372042 }
20382043 }
20392044
0 commit comments