Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
执行javascript异步函数并在selenium中返回结果_Javascript_Selenium_Async Await_Anonymous Function_Asynchronous Javascript - Fatal编程技术网

执行javascript异步函数并在selenium中返回结果

执行javascript异步函数并在selenium中返回结果,javascript,selenium,async-await,anonymous-function,asynchronous-javascript,Javascript,Selenium,Async Await,Anonymous Function,Asynchronous Javascript,我试图在selenium中执行以下脚本 result = driver.execute_script('let result; await axe.run().then((r)=> {result=r}); return result;') 但它正在回归 javascript错误:等待仅在异步函数中有效 我也试过了 result = @driver.execute_async_script('(async() => {return await axe.run();}

我试图在selenium中执行以下脚本

    result = driver.execute_script('let result; await axe.run().then((r)=> {result=r}); return result;')
但它正在回归

javascript错误:等待仅在异步函数中有效

我也试过了

    result = @driver.execute_async_script('(async() => {return await axe.run();})();')
但它返回了以下错误

Selenium::WebDriver::Error::ScriptTimeoutError:脚本超时:30秒内未收到结果


如果您想使用execute\u async\u脚本,您需要调用传递的回调函数来告诉驱动程序您完成了,否则驱动程序不会识别您完成了,并等待超时。回调函数是最后一个参数:

script = "
var callback = arguments[arguments.length - 1]; // this is the callback to call when you are done
axe.run().then((r)=> {callback(r)});
"
result = @driver.execute_async_script(script);
供参考