简体   繁体   English

如何自动填写简单表格并单击JavaScript中的提交以获取Chrome扩展程序

[英]How to automate filling a simple form and clicking submit in JavaScript for a chrome extension

Been trying to do this for days now, and have failed and have searched for examples but not found one yet that has helped. 数天以来一直在尝试执行此操作,但是失败并搜索了示例,但尚未找到有帮助的示例。

I want to navigate through a web page by filling in the details and clicking submit programmatically. 我想通过填写详细信息并以编程方式单击提交来浏览网页。

the page is as follows: 该页面如下:

<form method='post' action='login.php'>
    <table>
        <tr>
        <td>Email Address: </td>
        <td ><input type='Text' size='30' name='email'></td>
        </tr><tr>
        <td>Password: </td>
        <td ><input type='password' size='30' name='password'></td>
        </tr><tr>
        <td colspan=2>&nbsp;</td>
        </tr><tr>
        <td>&nbsp;</td>
        <td  align='left'><input type='Submit' value='Log in'></td>
        </tr>
        <td colspan=2>&nbsp;</td>
        <tr><td colspan=2><hr></td></tr>
    </table>
</form>

I cant change the above existing web page, So want to include code in a chrome extension written in JavaScript to save me heaps of time at the moment I cant get passed the login page! 我无法更改上述现有网页,因此,我想在无法通过登录页面的那一刻将代码包含在用JavaScript编写的chrome扩展程序中,以节省大量时间!

I have been a programmer in the distant past but am not fully versed with JavaScript syntax yet.. 我曾经是一个遥远的程序员,但还不完全了解JavaScript语法。

One of my problems is the form does not seem to have an ID. 我的问题之一是表格似乎没有ID。

Could anyone help? 有人可以帮忙吗?

Thanks for the help so far but I am still struggling so have added more detail. 到目前为止,感谢您的帮助,但我仍在努力,因此添加了更多详细信息。 I have managed to do this submit in IE using VB in the past (from an Excel macro) using: 过去,我已经使用VB在IE中使用VB在IE中进行了提交:

    Sub PressSubmit(IE)
    With IE.Document
    Set elems = .getElementsByTagName("input")
    For Each e In elems
        If (e.getAttribute("value") = "Log in") Then
            e.Click
            Exit For
        End If
    Next e

IE.Document.all("email").Value = "me@email.address"
IE.Document.all("password").Value = "myPassword"
Call PressSubmit(IE)

But now I need to do the same on Chrome and I am struggling with JavaScript syntax which is new to me is there anyone that could give me a JavaScript version of this, or a better version, that I can learn from by example? 但是现在我需要在Chrome上执行相同的操作,而我在JavaScript语法方面苦苦挣扎,这对我来说是新的,是否有人可以给我提供JavaScript版本或更好的版本,我可以从示例中学到吗?

You can use document.querySelector() , Element.querySelector() , attribute selectors to set values at elements, call .click() on element to submit ` 您可以使用document.querySelector() Element.querySelector() attribute selectors to set values at elements, call 。点击() on element to submit `

<!DOCTYPE html>
<html>

<head>
</head>

<body>
  <form method='post' action='login.php'>
    <table>
      <tr>
        <td>Email Address: </td>
        <td>
          <input type='Text' size='30' name='email'>
        </td>
      </tr>
      <tr>
        <td>Password: </td>
        <td>
          <input type='password' size='30' name='password'>
        </td>
      </tr>
      <tr>
        <td colspan=2>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td align='left'>
          <input type='submit' value='Log in'>
        </td>
      </tr>
      <tr>
        <td colspan=2>&nbsp;</td>
      </tr>
      <tr>
        <td colspan=2>
          <hr>
        </td>
      </tr>
    </table>
  </form>
  <script>
  var form = document.querySelector("form[action='login.php']");
  var submit = form.querySelector("input[type='submit'][value='Log in']");
  if (submit.length) {  
    form.querySelector("input[name='email']").value = "me@email.address";
    form.querySelector("input[type='password']").value = "myPassword";
    submit.click();
  }
  </script>
</body>

</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM