3

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:

  1. Is it possible to execute several tasks asynchronously with Selenium (here Click action & listener)?
  2. Why Selenium can't send Click action to the browser here?
  3. Do Selenium commands like Click, SendKeys execute in the main javascripts thread?

1 Answer 1

2

IMHO, if the logic is like that:

Asynchronous js function must be started to execute after the click on the button.

then you need to do something like a chain:

await Task.Run(() => { new Actions(browser).Click(button).Perform(); }).ContinueWith((_task) => { async code to execute AFTER Click (Perform method completion?) })

Sorry, not aware yet about intrinsic logic of Selenium, so not sure it's feasible.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.