2016-01-21 70 views
2

我試圖使用VBA打開IE的新實例,轉到網頁,登錄,然後導航到我可以預訂我的健身房課程的頁面,導航到正確的課&按一個'書'按鈕。使用VBA操作IE來登錄和預訂班級

我相當熟悉簡單的VBA,我已經使用了一段時間來自動執行excel任務,但是我從來沒有嘗試過將它與IE結合使用,所以我對它的相當新穎。
我不太瞭解VBA如何與HTML集成,而我的HTML知識是相當基礎的。

到目前爲止,我已成功地創建代碼,打開IE瀏覽器,瀏覽到正確的網頁,然後登錄我進去。 從那裏變得複雜...

該網頁有一系列的「塊'可以選擇幾周的健身課程。這些都是每天更新的,所以代碼需要搜索正確的日期,時間&類,然後按相應的書本按鈕。

我已經廣泛地搜索了最近幾天的解決方案,但找不到類似於我正在尋找的任何東西。

我到目前爲止的VBA如下。這適用於我的要求的第一部分。

Sub Login_Test() 
    ''This version successfully goes to puregym.com and logs into my account 

    'Outstanding'''''''''''''''''''''''''''''''''' 
    'Need to select class page 
    'Need to select correct date & class 
    'Need to book class 
    '''''''''''''''''''''''''''''''''''''''''''''' 

    ' open IE, navigate to the desired page and loop until fully loaded 
     Set ie = CreateObject("InternetExplorer.Application") 
     my_url = "http://www.puregym.com/" 

     With ie 
      .Visible = True 
      .navigate my_url 


     Do Until Not ie.Busy And ie.readyState = 4 
      DoEvents 
     Loop 

     End With 

    ' Input the userid and password 
     ie.Document.getElementById("edit-email").Value = "email address" 
     ie.Document.getElementById("edit-pincode").Value = "pin number" 

    ' Click the Submit button 
     ie.Document.getElementById("edit-submit").Click 

     Do Until Not ie.Busy And ie.readyState = 4 
      DoEvents 
     Loop 

    ' Navigate to bookings page 

     With ie 

     .navigate "http://www.puregym.com/members/bookings" 

     End With 



    End Sub 

我可能是錯的,但我相信我想運行的HTML的一個示例如下。我想模仿選擇課程並單擊書籍按鈕的操作。 (一旦我已經掌握了基礎知識,我希望VBA將類名,日期&時間與我將存儲在excel範圍內的列表進行比較,並且只有在範圍內才能預訂。但我很欣賞我需要走之前我跑步,所有的......)

   <div class="day column-1" data-date="Friday 22nd January" data- shortdate="Fri 22/1" data-index="1"> 
     <h2>Fri 22/1</h2> 

      <div id="tt_block_53_33253" class="three columns tt_block" data-date="2016-01-22" data-time="06:15"> 
     <div class="timetable-class limited-availability"> 
          <button class="icon-info">&nbsp;</button> 
       <div class="popout"> 
       <span class="weekly-close">&nbsp;</span> 
       <h1>FAST Tabata</h1> 
       <p class="timetable-info">This class takes interval training to the next level. 20 seconds high intense work followed by 10 seconds rest for 8 rounds, sound easy? Give this class a go, you won’t be disappointed.</p> 
            <img src="//www.puregym.com/sites/default/files/styles/square_thumbnail/public/classes/Misc20_0.jpg?itok=mnWQFaIU" alt="Example of FAST Tabata" /> 
           </div> 

      <header> 
       <h1>FAST Tabata</h1> 
       <h2> 
       06:15         - 06:45            </h2> 
      </header> 


          <div class="pt-name">PT name: <strong>Lewis Pratten</strong></div> 

      <span class="gym-name">Bristol Harbourside</span> 

          <div class="booking-buttons"> 
         <a href="/members/class-booking/book/53/33253" class="use-ajax secondary button">Book</a> 
     <span class="spaces-text">Spaces</span> 
    <span class="spaces-number">1</span> 

       </div> 

非常感謝任何人都必須提供的幫助。或者建議用不同的語言更容易嗎?從我讀也許Python或Java的將是一個更好的環境,要做到這一點,但我不熟悉,要麼...

回答

0

要獲取預訂按鈕,你可以這樣做:

Dim links, link 
     Set links = IE.Document.GetElementsByTagName("a") 
     For Each link In links 
     If link.innerText = "/members/class-booking/book/53/33253" Then 
      link.Click 
     End If 
     Next link 
+0

謝謝。明天我會試一試。 –

+0

link.innertext如何工作?因爲它似乎不是一個完整的鏈接 –

+0

不幸的是工作。 鏈接(當我手動打開時)顯示此 '[{「command」:「settings」,「settings」:{「basePath」:「\ /」,「pathPrefix」:「」,「ajaxPageState」 :{「theme」:「puregym_skin」,「theme_token」:「fQYzpnbZd522ytgURf23-w0dxvP_bnyCOlwifAZR4rE」}},「merge」:true},{「command」:「alert」,「text」:「您無權訪問頁面。「}]' 鏈接到完整的HTML在這裏[鏈接](https://www.dropbox.com/s/nye05az7u7wr3l7/PG%20Code.txt?dl=0) –