Skip to content

Commit f6c6776

Browse files
kblokMeir017
authored andcommitted
Page.ClickAsync should work with disabled javascript (#636)
* Page.ClickAsync should work with disabled javascript * Ops
1 parent dd101a7 commit f6c6776

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

lib/PuppeteerSharp.Tests/InputTests/InputTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ public async Task ShouldClickTheButton()
3333
Assert.Equal("Clicked", await Page.EvaluateExpressionAsync<string>("result"));
3434
}
3535

36+
[Fact]
37+
public async Task ShouldClickWithDisabledJavascript()
38+
{
39+
await Page.SetJavaScriptEnabledAsync(false);
40+
await Page.GoToAsync(TestConstants.ServerUrl + "/wrappedlink.html");
41+
await Task.WhenAll(
42+
Page.ClickAsync("a"),
43+
Page.WaitForNavigationAsync()
44+
);
45+
Assert.Equal(TestConstants.ServerUrl + "/wrappedlink.html#clicked", Page.Url);
46+
}
47+
3648
[Fact]
3749
public async Task ShouldClickOffscreenButtons()
3850
{

lib/PuppeteerSharp/ElementHandle.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,11 +457,16 @@ public Task<bool> IsIntersectingViewportAsync()
457457

458458
private async Task ScrollIntoViewIfNeededAsync()
459459
{
460-
var errorMessage = await ExecutionContext.EvaluateFunctionAsync<string>(@" async element => {
460+
var errorMessage = await ExecutionContext.EvaluateFunctionAsync<string>(@"async(element, pageJavascriptEnabled) => {
461461
if (!element.isConnected)
462462
return 'Node is detached from document';
463463
if (element.nodeType !== Node.ELEMENT_NODE)
464464
return 'Node is not of type HTMLElement';
465+
// force-scroll if page's javascript is disabled.
466+
if (!pageJavascriptEnabled) {
467+
element.scrollIntoView({block: 'center', inline: 'center', behavior: 'instant'});
468+
return false;
469+
}
465470
const visibleRatio = await new Promise(resolve => {
466471
const observer = new IntersectionObserver(entries => {
467472
resolve(entries[0].intersectionRatio);
@@ -472,7 +477,7 @@ private async Task ScrollIntoViewIfNeededAsync()
472477
if (visibleRatio !== 1.0)
473478
element.scrollIntoView({block: 'center', inline: 'center', behavior: 'instant'});
474479
return false;
475-
}", this).ConfigureAwait(false);
480+
}", this, Page.JavascriptEnabled).ConfigureAwait(false);
476481

477482
if (errorMessage != null)
478483
{

lib/PuppeteerSharp/Page.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ private Page(
304304
/// </summary>
305305
public bool IsClosed { get; private set; }
306306

307+
internal bool JavascriptEnabled { get; set; } = true;
307308
#endregion
308309

309310
#region Public Methods
@@ -893,7 +894,14 @@ public async Task<byte[]> PdfDataAsync(PdfOptions options)
893894
/// <returns>Task.</returns>
894895
/// <param name="enabled">Whether or not to enable JavaScript on the page.</param>
895896
public Task SetJavaScriptEnabledAsync(bool enabled)
896-
=> Client.SendAsync("Emulation.setScriptExecutionDisabled", new { value = !enabled });
897+
{
898+
if (enabled == JavascriptEnabled)
899+
{
900+
return Task.CompletedTask;
901+
}
902+
JavascriptEnabled = enabled;
903+
return Client.SendAsync("Emulation.setScriptExecutionDisabled", new { value = !enabled });
904+
}
897905

898906
/// <summary>
899907
/// Toggles bypassing page's Content-Security-Policy.

0 commit comments

Comments
 (0)