【问题标题】:Execute javascript async function and return result in selenium执行 javascript 异步函数并在 selenium 中返回结果
【发布时间】:2021-01-13 04:55:27
【问题描述】:

我正在尝试在 selenium 中执行以下脚本

    result = driver.execute_script('let result; await axe.run().then((r)=> {result=r}); return result;')

但它正在返回

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

我也试过了

    result = @driver.execute_async_script('(async() => {return await axe.run();})();')

但它返回了以下错误

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

【问题讨论】:

    标签: javascript selenium async-await anonymous-function asynchronous-javascript


    【解决方案1】:

    如果你想使用execute_async_script你需要调用传递过来的回调函数告诉驱动你已经完成了,否则驱动不会识别你已经完成并等待直到超时。回调函数是最后一个参数:

    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);
    

    供参考see

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      相关资源
      最近更新 更多