2017-01-20 30 views
0

嗨真的需要一些幫助在這裏。我想通過一個網站的webscrap結果,只有問題是當我輸入一個無效的輸入時,它會拋出一個java警報框,我無法使用VBA代碼來克服。請幫助檢測並點擊一個JavaScript通知vba

代碼如下

Sub TIN_VERIFY_TN() 
Dim ie As Object 
Dim form As Variant, button As Variant 
Dim pannumss As Characters 
Set ie = CreateObject("InternetExplorer.application") 

With ie 

.Visible = True 
.Navigate ("http://www.tnvat.gov.in/tamil/Tinverification.aspx") 
Do While ie.Busy: DoEvents: Loop 

countsss = WorksheetFunction.CountA(Range("A:A")) 
For X = 2 To countsss 
ie.Document.getelementsbyname("txt_tngst").Item.Value = Cells(X, 1) 

ie.Navigate "javascript:__doPostBack('LinkButton2','')" 

    Do While ie.Busy Or Not ie.ReadyState = READYSTATE_COMPLETE 
     DoEvents 
    Loop 

    Cells(X, 2) = ie.Document.getElementById("txtName").innerText 

    ie.Navigate ("http://www.tnvat.gov.in/tamil/Tinverification.aspx") 
    Do While ie.Busy: DoEvents: Loop 

    Next 
    End With 
    Set ie = Nothing 
End Sub 

給出想通這是創建MSGBOX

<Script language='javascript'>alert('Invalid TIN No');</script><script language='javascript'>document.getElementById('txt_tngst').focus();</script></form> 
     </body> 
    </HTML> 

非常感謝所有幫助我能爲我怎樣才能知道事件當警報已被觸發,我應該如何關閉警報一旦出現

+0

http://stackoverflow.com/questions/9486847/close-javascript-alert-using-vba-automation –

+0

http://stackoverflow.com/questions/ 18075762/vba-close-java-alerts-popups-with-name-message-from-web?rq = 1 –

+0

您能否提供任何有效的'TIN No'進行測試? – omegastripes

回答

0

嘗試上一個耳鼻喉科腳本執行的readyState之前.execCommand "stop"變得完整:

Sub TIN_VERIFY_TN() 

    Const READYSTATE_UNINITIALIZED = 0 
    Const READYSTATE_LOADING = 1 
    Const READYSTATE_LOADED = 2 
    Const READYSTATE_INTERACTIVE = 3 
    Const READYSTATE_COMPLETE = 4 

    Dim ie As Object 
    Dim form As Variant, button As Variant 
    Dim pannumss As Characters 
    Dim cnt As Long 
    Dim x As Long 
    Dim a 

    Set ie = CreateObject("InternetExplorer.application") 
    With ie 
     .Visible = True 
     cnt = WorksheetFunction.CountA(Range("A:A")) 
     For x = 2 To cnt 
      .Navigate "http://www.tnvat.gov.in/tamil/Tinverification.aspx" 
      Do While .busy Or Not .readyState = READYSTATE_COMPLETE: DoEvents: Loop 
      .document.getElementsByName("txt_tngst").Item.Value = Cells(x, 1) 
      .document.parentWindow.execScript "__doPostBack('LinkButton2','')", "javascript" 
      Do While .readyState < READYSTATE_INTERACTIVE: DoEvents: Loop 
      .document.execCommand "stop" 
      Do While .busy Or Not .readyState = READYSTATE_COMPLETE: DoEvents: Loop 
      a = Array(.document.getElementById("txtName")) 
      If TypeName(a(0)) <> "Null" Then Cells(x, 2) = a(0).innerText 
     Next 
    End With 

End Sub 
+0

嗨歐米茄。感謝代碼。該代碼現在似乎沒有執行提交功能。基本上下面的代碼現在不會加載結果.document.parentWindow.execScript「 –