Home > Net >  how to handle script time out for javascript executer for fetch call
how to handle script time out for javascript executer for fetch call

Time:05-16

I am executing the below javascript using selenium java script exceuter, I want to return the response fromt the fetch call and want to store it in java variable in code. But below code is showing script time out, any suggestions how can I achive above requirements ???

 String location = "!async function(){\n"  
                "let data = await fetch(\"https://raw.githubusercontent.com/IbrahimTanyalcin/LEXICON/master/lexiconLogo.png\")\n"  
                "    .then((response) => response.blob())\n"  
                "    .then(data => {\n"  
                "        return data;\n"  
                "    })\n"  
                "    .catch(error => {\n"  
                "        console.error(error);\n"  
                "    });\n"  
                "\n"  
                "console.log(data);\n"  
                "return data;\n"  
                "}();\n";


        Object str = js.executeAsyncScript(location);

CodePudding user response:

Use setTimeout()

function resolveAfter2Seconds() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('resolved');
    }, 2000);
  });
}

async function asyncCall() {
  console.log('calling');
  const result = await resolveAfter2Seconds();
  console.log(result);
  // expected output: "resolved"
}

async function

How to useexecuteAsyncScriptmethodinorg.openqa.selenium.JavascriptExecutor

CodePudding user response:

I'm not how to escape the quotes but the javascript should be:

let url = "https://..."
fetch(url).then(r => r.blob()).then(arguments[0])

arguments[0] is the callback, it needs to call that within 30 seconds or the timeout happens

  • Related