I am trying to handle the completion of asynchronous function in Selenium. Asynchronous js function must be started to execute after the click on the button.
Say listenFor(arguments[0]) listens for the end of the click handlers execution and it starts to listen it before the click action was dispatched. In the next function I am listening for click event before Click action using async/await of C#.
private async void ExecuteJsAsync(IActionElement button)
{
Console.WriteLine("Started async function");
var result = Task<bool>.Factory.StartNew(() =>
{
Console.WriteLine("Started to listen");
var script = "listenFor(arguments[0])";
var executor = (IJavaScriptExecutor)browser;
return executor.ExecuteAsyncScript(script);
});
Console.WriteLine("Before click");
new Actions(browser).Click(button).Perform();
Console.WriteLine("After click");
_result = await result;
}
But I am getting the next sequence of logs:
1. Started async function
2. Before Click
3. Started to listen
Here we can see that Click action has not been dispatched. And Question is:
- Is it possible to execute several tasks asynchronously with Selenium (here
Click action&listener)? - Why Selenium can't send
Click actionto the browser here? - Do Selenium commands like
Click,SendKeysexecute in the main javascripts thread?