Html 打开本地html文件后如何让页面自动单击表单按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22771895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I have a page automatically click a form button after opening local html file
提问by RaynoldPaul
My site has a page where I can parse specific logs by filling out form fields and pressing a submit button.Sometimes I have to parse 100 different logs(which are automatically downloaded by my site from another FTP location) so I'm trying to make it as automated as possible. I have created a file on my local PC named log.html with this content
我的站点有一个页面,我可以通过填写表单字段并按下提交按钮来解析特定日志。有时我必须解析 100 个不同的日志(我的站点从另一个 FTP 位置自动下载),所以我正在尝试它尽可能自动化。我在本地 PC 上创建了一个名为 log.html 的文件,其中包含此内容
<form action="MYSITEURL" method="POST" target="_blank" >
<input type="password" name="password" value="MYPASSWORD"/>
<input type="text" name="LogCommand" value="NAME-OF-THE-LOG-AND-LOCATION"/>
<input type="submit" value="Submit1stLog" />
</form>
When I open the log.html file on my local PC, it opens with Firefox and I can see 2 fields which are automatically filled with password and some other info that I need about the log file names and commands, and I have a Submit button. When I manually press Submit, the local file connects to my website and sends those values and submits it.Basically I fill out the form locally instead of going to my website. This way I can have 100 lines like the above one within one local HTML file and press 100 submit buttons manually, instead of going to my site 100 times.
当我在本地 PC 上打开 log.html 文件时,它会用 Firefox 打开,我可以看到 2 个字段,这些字段自动填充了密码以及我需要的有关日志文件名和命令的其他一些信息,并且我有一个提交按钮. 当我手动按提交时,本地文件连接到我的网站并发送这些值并提交它。基本上我在本地填写表单而不是去我的网站。通过这种方式,我可以在一个本地 HTML 文件中包含 100 行与上述类似的行,然后手动按下 100 个提交按钮,而不是访问我的网站 100 次。
My goal is to have some sort of JavaScript or something similar that will automatically click the Submit button for me as soon as I open the log.html local file.Is this possible? What if I have 100 Submit buttons?
我的目标是让某种 JavaScript 或类似的东西在我打开 log.html 本地文件时自动为我单击“提交”按钮。这可能吗?如果我有 100 个提交按钮怎么办?
I tried to put the following in log.html but it didn't work
我试图将以下内容放在 log.html 中,但没有用
<form action="MYSITEURL" method="POST" target="_blank" > <input type="password" name="password" value="MYPASSWORD"/> <input type="text" name="LogCommand" value="NAME-OF-THE-LOG-AND-LOCATION"/> <input type="submit" id="Submit1stLog" value="Submit1" /> </form>
<script>
function clickButton()
{
document.getElementById('Submit1stLog').click();
}
</script>
If you noticed,I added the id="Submit1stLog" part and then tried to get element ID. This didn't work and I'm terrible at this if you noticed so any help is appreciated !
如果您注意到了,我添加了 id="Submit1stLog" 部分,然后尝试获取元素 ID。这没有用,如果您注意到的话,我对此很糟糕,因此感谢您的帮助!
Thank you.
谢谢你。
回答by Khobar
Try to submit on page load :
尝试在页面加载时提交:
window.onload = function(){
document.forms['FORM_NAME'].submit()
}
or you can also click button on page load , but submiting form might be better idea
或者您也可以在页面加载时单击按钮,但提交表单可能是更好的主意
回答by user3879878
Here is the Code :
这是代码:
<form name="SITEURL" action="MYSITEURL" method="post" target="_blank" >
<input type="password" name="password" value="MYPASSWORD"/>
<input type="text" name="LogCommand" value="NAME-OF-THE-LOG-AND-LOCATION"/>
<input type="submit" id="Submit1stLog" value="Submit1" />
</form>
<script>
window.onload = function(){
document.forms('SITEURL').submit();
}
</script>