1

I have a Swagger authorization window and I would like to automatically fill the api key field:

enter image description here

I do it through js, and it works:

var element = document.querySelector('form div .wrapper section input');
element.value = "Bearer " + data.access_token;

But if I then click on the Authorize (form submit) button, nothing happens. The button works only if I enter some value in input manually, and it works even if I erase it then and leave input empty. I tried to trigger some events (change, keypress, keydown, etc) on input or on the form, but it didn't bring any results. How can I work around it?

Form html dom screen: enter image description here

imported_lis
  • 325
  • 3
  • 17
  • instead of queryselector have you tried getElementById()? Technically all the buttons could be an input type too. – Danimal Jul 07 '20 at 14:39
  • 1
    @Danimal This input doesn't have Id. And there are no problems with filling input value. Problem with the form's submit after it. – imported_lis Jul 07 '20 at 14:44
  • Can we get actual code of how you setup the form and its contents? And what function it calls when submitted – Tony Jul 07 '20 at 14:47
  • @Tony It's not my form, it's by Swagger. I additionally attached a html dom code screen, but there is also a large js file that I can't attach for obvious reasons. The js call stack on the submit form is very large and it is unvailable to debug it (I tried, really), so I would like to see all possible options for what it can be triggered by. – imported_lis Jul 07 '20 at 14:55
  • You need to use `preauthorizeApiKey` - see the linked Q&A. – Helen Jul 07 '20 at 16:01
  • 1
    I want to do similar stuff, but it is kind of custom header that I want to populate. But it does not work. I selected element using xpath (document.evaluate) and then set element.singleNodeValue.value; it shows value on UI but not picked when i click on 'Execute' – Vsh Dec 13 '21 at 10:06
  • @Vsh Reason why value is not picked is because react.js overrides .set() method on input element. To set value from pure js you have to obtain original set method and fire 'input' event, check my answer from similar question: https://stackoverflow.com/a/72486112/4695799 – Padvinder Jun 10 '22 at 07:55

0 Answers0