executeAsyncScript() 脚本超时以获取 post 调用

script time out with executeAsyncScript() for fetch post call

我正在尝试 运行 使用具有获取调用的 executeAsyncScript() 方法的脚本。因为 fetch 调用 returns 一个承诺,因此在控制台上它需要一些时间来实现承诺但是使用 selenium java 脚本执行器它抛出错误说脚本超时,因此我得到 null 作为输出。如何使用 selenium executeAsyncScript 方法达到预期结果。

String str = (String) js.executeAsyncScript("var myHeaders = new Headers();\n" +
                "myHeaders.append('client-id', 'LPDP');\n" +
                "myHeaders.append('a2z-csrf-token', 'NON_SDF');\n" +
                "myHeaders.append('x-amz-rid', 'M6507NCWPW2FVPSSRMVM');\n" +
                "\n" +
                "let inputEntities = new Map();\n" +
                "inputEntities.set(\"Commons$customerId\", \"\\"A2ZLDCQRXMMNLG\\"\")\n" +
                "inputEntities.set(\"Commons$marketPlaceId\", \"\\"A2XZLSVIQ0F4JT\\"\")\n" +
                "inputEntities.set(\"Commons$sessionId\", \"\\"asdb3412\\"\")\n" +
                "inputEntities.set(\"Commons$ubId\", \"\\"abc\\"\")\n" +
                "inputEntities.set(\"Rewards$APPA$Commons$eventId\", \"\\"prsrohitest-1\\"\")\n" +
                "inputEntities.set(\"Rewards$APPA$Commons$clientId\", \"\\"HFC\\"\")\n" +
                "inputEntities.set(\"Rewards$APPA$Commons$useCaseName\", \"\\"lineItemPromotionPaymentMethodEvent\\"\")\n" +
                "inputEntities.set(\"Rewards$APPA$Commons$eventTimeStamp\",\"\\"2022-04-20T21:21:57.934Z\\"\" )\n" +
                "inputEntities.set(\"Rewards$APPA$Commons$category\", \"\\"HFC\\"\")\n" +
                "inputEntities.set(\"Rewards$APPA$Commons$subCategory\", \"\\"PREPAID_RECHARGE\\"\")\n" +
                "inputEntities.set(\"Rewards$APPA$Commons$requestType\", \"\\"HFCBP\\"\")\n" +
                "inputEntities.set(\"Rewards$APPA$Commons$partition\", \"\\"useCaseName,category,subCategory\\"\")\n" +
                "inputEntities.set(\"Rewards$APPA$Commons$benefitsToBeEvaluated\", \"[\\"GCCashback\\",\\"Coupon\\",\\"Membership\\",\\"ScratchCard\\"]\")\n" +
                "\n" +
                "let entitiesToBeResolved = [\"Rewards$APPA$GetAllPromotions$applicablePromotionDetailList\"]\n" +
                "\n" +
                "const executeInput = {\n" +
                "\"inputEntities\": Object.fromEntries(inputEntities),\n" +
                "\"entitiesToBeResolved\": entitiesToBeResolved,\n" +
                "};\n" +
                "\n" +
                "var obj \n" +
                "\n" +
                "fetch("url", {\n" +
                "  method: 'POST',\n" +
                "  headers: myHeaders,\n" +
                "  body: JSON.stringify(executeInput),\n" +
                "})\n" +
                "  .then(response => response.text())\n" +
                "  .then(result => obj = result)\n" +
                "  .then(()=> console.log(obj))\n" +
                "  .catch(error => console.log('error', error));\n" +
                "\n" +
                "  return obj;");

我在 str 变量中得到 null。在此先感谢您的帮助

注意:我不习惯使用java所以我不知道如何正确转义字符串。

一般的做法是:

js.executeAsyncScript("arguments[0]('foo')")

你可以把它放在 promise 的 then 里面,它仍然有效。