如何让浏览器自动访问 1000 个页面并在每个页面上触发一个简单的 javascript 函数?
我需要在跟踪/报告应用程序上加载 1000 个 url(通过 HTML 表单进行身份验证后)并触发“重新提交”javascript 函数。不幸的是,没有批量操作可以同时处理所有操作,所以我只能使用自动化。我有什么选择?
http://domain.com/0001.php
http://domain.com/0002.php
http://domain.com/0003.php
...
http://domain.com/1000.php
上述每个页面都有一个由 href 触发的 resubmit() javascript 函数。我怎样才能自动触发这些?
示例:
<form action="/resubmit" method="POST">
<input type="hidden" name="security_token" value="SUPER-LONG-HASH">
<input type="hidden" name="url" value="http://mysite.com/0001.html">
<input type="hidden" name="redirect" value="long-string">
<script type="text/javascript">
window["resubmit"] = function () {
document["resubmit"].submit();
return false;
}
</script>
<a href="javascript:resubmit()" class="resubmit-class">resubmit</a>
</form>
我使用的是 Mac。 Unix、Perl、Bash、PHP、Automator、FireFox iMarcos 均可用。
I need to load 1000 urls on a tracking/reporting app (after authenticating via an HTML form) and trigger a "resubmit" javascript function. Unfortunately there is no bulk action to handle all at once so I'm left with automation. What are my options?
http://domain.com/0001.php
http://domain.com/0002.php
http://domain.com/0003.php
...
http://domain.com/1000.php
Each of the above pages has a resubmit() javascript function triggered by a href. How can I automate triggering these?
Example:
<form action="/resubmit" method="POST">
<input type="hidden" name="security_token" value="SUPER-LONG-HASH">
<input type="hidden" name="url" value="http://mysite.com/0001.html">
<input type="hidden" name="redirect" value="long-string">
<script type="text/javascript">
window["resubmit"] = function () {
document["resubmit"].submit();
return false;
}
</script>
<a href="javascript:resubmit()" class="resubmit-class">resubmit</a>
</form>
I'm on a Mac. Unix, Perl, Bash, PHP, Automator, FireFox iMarcos are all available.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该查看 PhantomJS,“带有 JavaScript API 的无头 WebKit”。它允许您从命令行运行 WebKit 浏览器实例并执行 Javascript。
您也许可以使用 Pjscrape 节省一些时间,这是一个构建在 PhantomJS 之上的工具,可以抓取多个页面或获取一长串 URL(免责声明:这是我的项目)。我还没有尝试过 1,000 多个 URL,但我认为您可以使用以下 6 行来执行您所描述的操作:
You should check out PhantomJS, "a headless WebKit with JavaScript API". It allows you to run a WebKit browser instance from the command line and execute Javascript.
You might be able to save some time using Pjscrape, a tool built on top of PhantomJS that can spider multiple pages or take a long list of URLs (disclaimer: this is my project). I haven't tried it with 1,000+ URLs, but I think you could do what you describe with the following 6 lines:
我已经对其他答案进行了投票,但最终我直接使用了 AppleScript。这很有帮助,因为它使用现有会话,因此我不必处理任何身份验证问题。感谢大家的帮助。我期待着熟悉您分享的工具。
I already voted up the other answers, but in the end I wound up using straight AppleScript. This was helpful because it used an existing session so I didn't have to deal with any authentication problems. Thanks for all of your help, everyone. I look forward to becoming familiar with the tools you shared.
我会使用 Ruby+Watir 来实现这一点。示例代码(未测试):
I would use Ruby+Watir for that. Example code (not tested):
我不知道这是否对您有帮助,但您可以尝试Fake。我认为它可以让您自动提交表单并进行循环。
I don't know if this will help you, but you could try Fake. I think it would allow you to automate submitting the form and make a loop.