55using PuppeteerSharp . Input ;
66using PuppeteerSharp . Messaging ;
77using System ;
8+ using System . Collections . Generic ;
89using System . IO ;
910using System . Linq ;
1011using System . Threading . Tasks ;
@@ -426,16 +427,20 @@ public Task<bool> IsIntersectingViewportAsync()
426427 {
427428 GetContentQuadsResponse result = null ;
428429
430+ var contentQuadsTask = Client . SendAsync < GetContentQuadsResponse > ( "DOM.getContentQuads" , new DomGetContentQuadsRequest
431+ {
432+ ObjectId = RemoteObject . ObjectId
433+ } ) ;
434+ var layoutTask = Client . SendAsync < PageGetLayoutMetricsResponse > ( "Page.getLayoutMetrics" ) ;
435+
429436 try
430437 {
431- result = await Client . SendAsync < GetContentQuadsResponse > ( "DOM.getContentQuads" , new DomGetContentQuadsRequest
432- {
433- ObjectId = RemoteObject . ObjectId
434- } ) . ConfigureAwait ( false ) ;
438+ await Task . WhenAll ( contentQuadsTask , layoutTask ) . ConfigureAwait ( false ) ;
439+ result = contentQuadsTask . Result ;
435440 }
436441 catch ( Exception ex )
437442 {
438- _logger . LogError ( ex . Message ) ;
443+ _logger . LogError ( ex , "Unable to get content quads" ) ;
439444 }
440445
441446 if ( result == null || result . Quads . Length == 0 )
@@ -444,7 +449,11 @@ public Task<bool> IsIntersectingViewportAsync()
444449 }
445450
446451 // Filter out quads that have too small area to click into.
447- var quads = result . Quads . Select ( FromProtocolQuad ) . Where ( q => ComputeQuadArea ( q ) > 1 ) ;
452+ var quads = result . Quads
453+ . Select ( FromProtocolQuad )
454+ . Select ( q => IntersectQuadWithViewport ( q , layoutTask . Result ) )
455+ . Where ( q => ComputeQuadArea ( q . ToArray ( ) ) > 1 ) ;
456+
448457 if ( ! quads . Any ( ) )
449458 {
450459 throw new PuppeteerException ( "Node is either not visible or not an HTMLElement" ) ;
@@ -466,6 +475,13 @@ public Task<bool> IsIntersectingViewportAsync()
466475 ) ;
467476 }
468477
478+ private IEnumerable < BoxModelPoint > IntersectQuadWithViewport ( IEnumerable < BoxModelPoint > quad , PageGetLayoutMetricsResponse viewport )
479+ => quad . Select ( point => new BoxModelPoint
480+ {
481+ X = Math . Min ( Math . Max ( point . X , 0 ) , viewport . ContentSize . Width ) ,
482+ Y = Math . Min ( Math . Max ( point . Y , 0 ) , viewport . ContentSize . Height ) ,
483+ } ) ;
484+
469485 private async Task ScrollIntoViewIfNeededAsync ( )
470486 {
471487 var errorMessage = await ExecutionContext . EvaluateFunctionAsync < string > ( @"async(element, pageJavascriptEnabled) => {
0 commit comments