簡體   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