【问题标题】:Button Click Does not works without Keyboard/Human interruption按钮单击在没有键盘/人工中断的情况下不起作用
【发布时间】:2020-07-20 04:59:26
【问题描述】:

场景:开发者通过调用外部服务生成JWT Token,生成jwt后,复制粘贴到Swagger的授权部分,点击授权按钮,之后我们可以从swagger中触发API,因为它现在有jwt Token。

我已经尝试自动化它,一旦生成 JWT 我的 JS 代码将在授权部分输入 jwt 令牌并尝试单击授权按钮。但是这里有一些问题,我可以看到 JS 正在尝试单击授权按钮(在控制台中我可以看到该操作)但没有发生任何事情。我还注意到,如果我们从键盘手动添加任何内容,然后按下按钮,它就会起作用。如何在 JS 中模拟这个?

注意:如果我们手动按下/输入键盘上的任何字符,然后触发 z.click() 是否有效?我们如何在 JS 中模拟输入文本的键盘按下?

我试过了,但没有成功:

 document.getElementById("va").dispatchEvent(
 new KeyboardEvent("keydown", {
 key: "e",
 keyCode: 69, // example values.
 code: "KeyE", // put everything you need in this object.
 which: 69,
 shiftKey: false, // you don't need to include values
 ctrlKey: false,  // if you aren't going to use them.
 metaKey: false   // these are here for example's sake.
 })
);



 const txtToken = document.getElementById("jwttoken");

function simulateEnterToken (x,tokens){
var val=x.getElementsByClassName("dialog-ux")[0]
             .getElementsByClassName("modal-ux")[0]
              .getElementsByClassName("modal-dialog-ux")[0]
              .getElementsByClassName("modal-ux-inner")[0]
              .getElementsByClassName("modal-ux-content")[0]
             .getElementsByClassName("auth-container")[0] .getElementsByClassName("wrapper")[1];
   val.getElementsByTagName("input")[0].id="va";
  document.getElementById("va").value=tokens;

 console.log(x);
}


function simulateClickAuthorize(x){
 var y=x.getElementsByClassName("dialog-ux")[0]
         .getElementsByClassName("modal-ux")[0]
          .getElementsByClassName("modal-dialog-ux")[0]
          .getElementsByClassName("modal-ux-inner")[0]
          .getElementsByClassName("modal-ux-content")[0]
         .getElementsByClassName("auth-container")[0]
          .getElementsByClassName("auth-btn-wrapper")[0];
           console.log(y);
       var z=y.getElementsByClassName("btn modal-btn auth authorize button")[0];
   z.click();
    }

添加了 2 个截图以便清晰理解

注意:我也尝试过模拟键盘事件

document.getElementById("va").value.dispatchEvent(new 
KeyboardEvent('keypress',{'key':'a'}));


 document.getElementById("va").dispatchEvent(
 new KeyboardEvent("keydown", {
  key: "e",
  keyCode: 69, // example values.
  code: "KeyE", // put everything you need in this object.
  which: 69,
  shiftKey: false, // you don't need to include values
  ctrlKey: false,  // if you aren't going to use them.
  metaKey: false   // these are here for example's sake.
  })
 );

我也不能改变任何东西,因为它是 3rd 方库!

编辑:附加 Console.log(z) 的输出

编辑 2:表单没有 ID

编辑 3:@Roger 要求提交表单

var val=x.getElementsByClassName("dialog-ux")[0]
             .getElementsByClassName("modal-ux")[0]
              .getElementsByClassName("modal-dialog-ux")[0]
              .getElementsByClassName("modal-ux-inner")[0]
              .getElementsByClassName("modal-ux-content")[0]
             .getElementsByClassName("auth-container")[0]

val.getElementsByTagName("form")[0].submit()

它也没有工作,我得到错误页面

错误

白标错误页面 此应用程序没有 /error 的显式映射,因此您将其视为后备。Mon Jul 20 14:01:11 IST 2020 出现意外错误(类型=错误请求,状态=400)。 必需的字符串参数“产品”不存在

编辑 4:这是我的初始网址

http://localhost:8080/docs?product=test&docid=testapp-swagger-openapi&type=yaml#/person-controller/getPersonUsingGET

点击 submit() 后 url 出现错误

http://localhost:8080/docs?#/person-controller/getPersonUsingGET

点击提交()后的图片/网络控制台

注意:手动点击时没有api调用

【问题讨论】:

  • console.log(z) 返回什么?
  • @DarrenSweeney 我已在 EDIT 中附加了 console.log(z) 的输出

标签: javascript dom-events ui-automation keyboard-events


【解决方案1】:

为了提交您的表单,我相信您会想要定位您的表单元素并调用submit 方法。

示例(假设页面上只有一个 <form> 元素):

var formElement = document.getElementByTagName('form')[0];
formElement.submit();

【讨论】:

  • 所以我需要获取表单的 id 并提交对吗?但是表单没有id,我在EDIT2中附上了截图
  • 更新了答案,使用document.getElementByTagName定位表单元素。
  • 我设法获取表单并尝试提交,它返回错误。我添加了有问题的错误 EDIT var val=x.getElementsByClassName("dialog-ux")[0] .getElementsByClassName( "modal-ux")[0] .getElementsByClassName("modal-dialog-ux")[0] .getElementsByClassName("modal-ux-inner")[0] .getElementsByClassName("modal-ux-content")[0 ] .getElementsByClassName("auth-container")[0] val.getElementsByTagName("form")[0].submit().
  • 从错误消息来看,您可能正确地调用了表单的处理程序,但在处理程序中抛出了一个错误。
  • 但是当我手动(用手)进行操作时,我的意思是当我在输入文本中输入内容并按下授权时它不会出错,我们是否遗漏了什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-28
  • 2017-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多