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

发布于 2024-12-12 02:39:34 字数 967 浏览 3 评论 0原文

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

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉风有信 2024-12-19 02:39:34

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

您也许可以使用 Pjscrape 节省一些时间,这是一个构建在 PhantomJS 之上的工具,可以抓取多个页面或获取一长串 URL(免责声明:这是我的项目)。我还没有尝试过 1,000 多个 URL,但我认为您可以使用以下 6 行来执行您所描述的操作:

pjs.addSuite({
    urls: [...], // your very long list here
    scraper: function() {
        window.resubmit();
    }
});

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:

pjs.addSuite({
    urls: [...], // your very long list here
    scraper: function() {
        window.resubmit();
    }
});
暖伴 2024-12-19 02:39:34

我已经对其他答案进行了投票,但最终我直接使用了 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

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.

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
转角预定愛 2024-12-19 02:39:34

我会使用 Ruby+Watir 来实现这一点。示例代码(未测试):

require "watir-webdriver"
browser = Watir::Browser.new :firefox

urls = ["http://domain.com/0001.php", "http://domain.com/0002.php"] # add more URLs here
urls.each do |url|
  browser.goto url
  browser.a(:text => "resubmit").click
end

I would use Ruby+Watir for that. Example code (not tested):

require "watir-webdriver"
browser = Watir::Browser.new :firefox

urls = ["http://domain.com/0001.php", "http://domain.com/0002.php"] # add more URLs here
urls.each do |url|
  browser.goto url
  browser.a(:text => "resubmit").click
end
饭团 2024-12-19 02:39:34

我不知道这是否对您有帮助,但您可以尝试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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文