如何使浏览器自动访问1000个页面并在每个页面上触发一个简单的javascript函数?

如何使浏览器自动访问1000个页面并在每个页面上触发一个简单的javascript函数?,javascript,browser,automation,browser-automation,Javascript,Browser,Automation,Browser Automation,我需要在跟踪/报告应用程序上加载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()javascri

我需要在跟踪/报告应用程序上加载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>

窗口[“重新提交”]=函数(){
文件[“重新提交”]。提交();
返回false;
}
我在Mac电脑上。Unix、Perl、Bash、PHP、Automator、FireFox iMarcos都可以使用。

您应该查看“一个带有JavaScript API的无头WebKit”。它允许您从命令行运行WebKit浏览器实例并执行Javascript

您可以使用PhantomJS之上构建的工具节省一些时间,该工具可以抓取多个页面或获取一长串URL(免责声明:这是我的项目)。我还没有尝试过1000多个URL,但我认为您可以使用以下6行代码来实现您所描述的功能:

pjs.addSuite({
    urls: [...], // your very long list here
    scraper: function() {
        window.resubmit();
    }
});
我会使用Ruby+来实现这一点。示例代码(未测试):


我不知道这是否对你有帮助,但你可以试试。我认为这将允许您自动提交表单并进行循环。

我已经对其他答案投了赞成票,但最后我使用了直接的AppleScript。这很有帮助,因为它使用了一个现有的会话,所以我不必处理任何身份验证问题。谢谢大家的帮助。我期待着熟悉您共享的工具

set thePath to (path to desktop as Unicode text) & "list_of_urls.txt"
set theFile to (open for access file thePath)
set theContent to (read theFile)
close access theFile

set theURLs to every paragraph of theContent

tell application "Safari"
    repeat with theURL in theURLs
        make new document
        set URL of front document to theURL
        delay 5
        set theScript to "document.getElementsByClassName('resubmit-class')[0].click();"
        do JavaScript theScript in current tab of first window
        do JavaScript "window.resubmit()" in front document
        delay 5
        close front document
    end repeat
end tell

你想过了吗?你能直接调用重新提交将调用的每个页面上的url吗?为什么不能在每个页面上运行onload函数重新提交?更新了问题以提供实际运行的脚本的更多上下文。出于好奇,为什么我对这个问题投了很多反对票?谢谢,尼克。你能告诉我如何首先通过Pjscrape登录到webapp吗?我没有看到任何关于这个的文档。不过,对于纯公共页面来说,它非常有效。对未来的项目绝对有用。我只是希望我能为这件事找到答案。也许是使用bookmarklet?嗯,你没有提到你需要身份验证——这肯定会使事情复杂化。HTTP auth,还是应用程序中的登录表单?它是HTML登录表单。是否可以通过bookmarklet从浏览器内部执行此操作?我对如何激活它并在控制台中使用感到有点困惑。什么控制台?我在哪里可以看到这个?再次感谢你的帮助。假的很有趣。我已经在一个页面上成功提交了javascript,但还没有弄清楚如何循环URL。有什么建议吗?我没有在其中尝试过多个URL,但我的理解是它只是运行JS,所以你应该能够创建一个数组并在其中循环?感谢分享你的解决方案-我认为你是对的,身份验证可能是这里最棘手的问题。谢谢Nick。不过我有兴趣了解PJScrae。似乎是我腰带上有用的工具。谢谢你。
set thePath to (path to desktop as Unicode text) & "list_of_urls.txt"
set theFile to (open for access file thePath)
set theContent to (read theFile)
close access theFile

set theURLs to every paragraph of theContent

tell application "Safari"
    repeat with theURL in theURLs
        make new document
        set URL of front document to theURL
        delay 5
        set theScript to "document.getElementsByClassName('resubmit-class')[0].click();"
        do JavaScript theScript in current tab of first window
        do JavaScript "window.resubmit()" in front document
        delay 5
        close front document
    end repeat
end tell