繁体   English   中英

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

[英]How can I automate a browser to visit 1000 pages and trigger a simple javascript function on each page?

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

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

您可以使用Pjscrape节省一些时间, Pjscrape是一个构建在PhantomJS之上的工具,它可以捕获多个页面或获取一长串URL(免责声明:这是我的项目)。 我没有尝试过1000多个网址,但我认为你可以用以下6行做你所描述的:

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

我已经投了其他答案,但最后我还是使用了直接的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

我会使用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

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

暂无
暂无

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

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