使用IE浏览时处理网站弹出

时间:2016-06-23 22:24:27

标签: javascript html excel-vba web automation

我有一个Excel VBA宏

  1. 打开IE,
  2. 导航到Medicare网站,
  3. 登录我,
  4. 将网站上列出的声明与工作簿中已有的声明进行比较
  5. 提醒我任何差异
  6. 在登录步骤中我遇到了问题,所以我在下面复制了我的代码部分。执行.click行后,会出现一个弹出窗口,要求用户单击确定按钮才能继续。宏执行暂停,直到我手动点击弹出窗口中的确定按钮。

    http://www.mymedicare.gov网页背后的源代码包含有关弹出窗口的信息,但我无法弄清楚如何使用它以便我可以通过编程方式单击弹出窗口确定按钮。

    几年前,我发布了this question,Tim Williams基于与弹出窗口后面的javascript进行交互,提供了一个出色的solution。 Medicare已经改变了网页背后的代码,Tim的解决方案不再处理弹出窗口。我在Tim的主题上尝试了很多变化,但还没有找到解决方案。

    在确定如何以编程方式点击弹出窗口的确定按钮方面的任何帮助将非常感激。

      

    注意:出于此问题的目的,可以使用任何用户ID和密码(我在下面的代码中输出了abcde和12345)。如果你处理弹出窗口,你将被传递到一个页面,上面写着不正确的用户ID /密码。这表明您已成功处理弹出窗口。

    Sub Medicare_Claims()' 
    ' Update the status bar 
      Application.StatusBar = "Running the Medicare Claims subroutine" 
    
    ' Open the "MyMedicare web page 
        Set ie = CreateObject("InternetExplorer.Application") 
        With ie 
            .Visible = True 
            .Navigate "https://www.mymedicare.gov/" 
        End With 
    
    ' Loop until the page is fully loaded 
        Do Until ie.ReadyState = 4 And Not ie.Busy 
            DoEvents 
        Loop 
    
    ' Log-in 
        ie.Document.all.Item("ctl00_ContentPlaceHolder1_ctl00_HomePage_SWEUserName").Value = "abcde"           
        ie.Document.all.Item("ctl00_ContentPlaceHolder1_ctl00_HomePage_SWEPassword").Value = "12345"     
        ie.Document.getElementById("ctl00_ContentPlaceHolder1_ctl00_HomePage_SignIn").Click
    
    ' Loop until the page is fully loaded 
        Do Until ie.ReadyState = 4 And Not ie.Busy 
            DoEvents 
        Loop 
    
        Application.Wait (Now + TimeValue("0:00:15")) 
    
    ' Navigate further to the Search Claims" web page 
        ie.Navigate "https://www.mymedicare.gov/searchclaims.aspx" 
    

1 个答案:

答案 0 :(得分:1)

这对我有用

Sub Medicare_Claims() '
' Update the status bar
  Application.StatusBar = "Running the Medicare Claims subroutine"

' Open the "MyMedicare web page
    Set ie = CreateObject("InternetExplorer.Application")
    With ie
        .Visible = True
        .Navigate "https://www.mymedicare.gov/"
    End With

' Loop until the page is fully loaded
    Do Until ie.ReadyState = 4 And Not ie.Busy
        DoEvents
    Loop

' Log-in
    Dim doc As Object

    Set doc = ie.Document

    doc.getElementById("ctl00_ContentPlaceHolder1_ctl00_HomePage_SWEUserName").Value = "abcde"
    doc.getElementById("ctl00_ContentPlaceHolder1_ctl00_HomePage_SWEPassword").Value = "12345"
    doc.getElementById("ctl00_ContentPlaceHolder1_ctl00_HomePage_Agree").Value = "True"

    doc.parentWindow.execScript "window.ConfirmationPopup = function(){null;}", "jscript"

    doc.getElementById("ctl00_ContentPlaceHolder1_ctl00_HomePage_SignIn").Click

End Sub