最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Use JavaScript to automatically fill in HTML forms - Stack Overflow

programmeradmin1浏览0评论

I'm trying to automate a test routine in a web application. My goal is to fill forms, submit them and get the html returned by the application after the form is submited. In other words, i want to simulate what a human would do, but as i need to do this in dozens of forms, i want to do it automatically using pure JS (no frameworks).

For instance, a login form i'm trying to submit looks like this :

To fill the form i'm using a code like this :

document.getElementById('username').value = '[email protected]'; document.getElementById('password').value = 'mypassord';

But it seems the information is not being correctly filled ; after i run the code above, the form looks like this :

As you can see, the placeholder is still on the inputs and if i submit the form, the input values are not submited. It seems the 'value = 'xxx'' is not effectively filling the field.

It only works if i manually input something on the keyboard after running the code above.

What else can i try ?

Thanks !

I'm trying to automate a test routine in a web application. My goal is to fill forms, submit them and get the html returned by the application after the form is submited. In other words, i want to simulate what a human would do, but as i need to do this in dozens of forms, i want to do it automatically using pure JS (no frameworks).

For instance, a login form i'm trying to submit looks like this :

To fill the form i'm using a code like this :

document.getElementById('username').value = '[email protected]'; document.getElementById('password').value = 'mypassord';

But it seems the information is not being correctly filled ; after i run the code above, the form looks like this :

As you can see, the placeholder is still on the inputs and if i submit the form, the input values are not submited. It seems the 'value = 'xxx'' is not effectively filling the field.

It only works if i manually input something on the keyboard after running the code above.

What else can i try ?

Thanks !

Share Improve this question edited Feb 11, 2019 at 10:25 delphirules asked Feb 10, 2019 at 10:09 delphirulesdelphirules 7,50818 gold badges69 silver badges132 bronze badges 1
  • try changing the placeholder to '' – Maheer Ali Commented Feb 10, 2019 at 10:17
Add a ment  | 

1 Answer 1

Reset to default 3

Here is the plete code. Run on your browser hope it will works for you.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>

<body>
  <input type="text" placeholder="This is text Field" id="txtField">
  <input type="password" placeholder="Input password" id="txtPass">
  <script>
    document.getElementById('txtField').value = '[email protected]';
    document.getElementById('txtPass').value = 'test@123';
  </script>
</body>

</html>

发布评论

评论列表(0)

  1. 暂无评论