microsoft / playwright-dotnet

.NET version of the Playwright testing and automation library.

Home Page:https://playwright.dev/dotnet/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug]: Click is not working via javascript ExecuteAsync

atif87 opened this issue · comments

Version

1.41

Steps to reproduce

Example steps (replace with your own):

  1. Use the following code :
using var playwright = await Playwright.CreateAsync();
var chrome = playwright.Chromium;
var browser = await chrome.LaunchAsync(new() { Headless = false });
var page = await browser.NewPageAsync();
await page.GotoAsync("https://www.bing.com/maps?FORM=Z9LH2&cp=35.684727%7E139.869426&lvl=18.7");
await page.Locator("//*[@data-tag=\"TaskBar.DirectionsButton\"]").ClickAsync();
await page.Locator("//*[@data-tag=\"watermarkInput\"]").First.FillAsync("coffee");
await page.Locator("//*[@data-tag=\"watermarkInput\"]").Last.FillAsync("pizza");
var script = "() => { (function () { var el = document.evaluate(\"//div[contains(@data-tasklayout,'defaultLayout')]//div[@data-containerareatype='cardsContainer']//div[@data-taskid='ts$$1']//*[@data-tag='directionsPanelRoot']//*[@data-tag='directionsPanel'][1]//*[@data-tag='directionsInput']//*[@data-tag='dirBtnGo']\", document, null, 9, null).singleNodeValue; var bounds = el.getBoundingClientRect(); var myEvt = document.createEvent('MouseEvents'); myEvt.initMouseEvent('click', true, true, window, 1, 0, 0, 0 + bounds.left, 0 + bounds.top, false, false, false, false, 0, null); el.dispatchEvent(myEvt); return; })() }";

Task task = page.EvaluateAsync(script);


await page.Locator("//*[@data-tag=\"watermarkInput1\"]").First.FillAsync("coffee");
Thread.Sleep(1000);
await browser.CloseAsync();

There will be no direction appearing on the screen

Expected behavior

I was expecting Go button clicked and a path appeared on the map.

Actual behavior

Go Button is not clicked and there is no path on the map

Additional context

We have used javascript to click. In the earlier version (1.36) it is working fine. But in the version 1.41 and 1.40 its not working.

Environment

- Operating System: [Windows]
- CPU: [arm64]
- Browser: [Chromium, ]
- .NET Version (TFM): [net8.0]
- Other info:

We are unfortunately not able to reproduce it, since the site probably behaves different based on the geolocation information. The xpath was not resolving to an element, after fixing this, the click was working, but there is a delay required between the fill and the click. I recommend sticking to Playwright API methods: ClickAsync and FillAsync in order to interact with elements. They retry over and over again to make sure a button is clickable before clicking on the actual element. In this case your artificial click is most likely being made when the button is not ready yet.

See here: https://playwright.dev/docs/actionability

Closing as per above.