以网页上显示的内容吸引程序

时间:2018-10-01 16:24:07

标签: explorer

我想构建一个与网页交互的程序,例如:

当A线变为绿色时,浏览器中显示了3行A,B和C 我想要一些变量值。以此类推。 我不想在我的代码中打开该页面,该页面已经在Windows资源管理器中打开,并且我希望我的代码与它所在的位置进行交互。

我希望我清楚地解释了我的问题。

1 个答案:

答案 0 :(得分:0)

我尝试使用下面的VBA代码访问已打开的网页,但无法获取文本。

Sub demo()
Dim IEWindows           As SHDocVw.ShellWindows
    Dim IEwindow            As SHDocVw.InternetExplorer
    Dim IEDocument          As MSHTML.HTMLDocument
    Dim BreadcrumbDiv       As MSHTML.HTMLElementCollection
    
    Set IEWindows = New SHDocVw.ShellWindows
 
    For Each IEwindow In IEWindows
    'Debug.Print (IEwindow.LocationURL)
        If InStr(IEwindow.LocationURL, "file:///C:/Users/Administrator/Desktop/demo17.html") <> 0 Then  ' Found it
           Set IEDocument = IEwindow.Document
           Set BreadcrumbDiv = IEDocument.getElementById("demo1")
           
         
            Debug.Print (IEwindow.Document.getElementById("data2").Value)
        End If
    Next
End Sub

作为解决方法,您可以尝试参考以下示例。

Sub demo()

    Dim myIE As Object
    Dim myIEDoc As Object

    'Start Internet Explorer
    Set myIE = CreateObject("InternetExplorer.Application")

    'if you want to see the window set this to True
    myIE.Visible = True

    'Now we open the page we'd like to use as a source for information
    myIE.navigate "C:\Users\Administrator\Desktop\demo17.html"

    'We wait for the Explorer to actually open the page and finish loading
    While myIE.Busy
        DoEvents
    Wend

    'Now lets read the HTML content of the page
    Set myIEDoc = myIE.Document

        'Then we'll get something from teh inner page content by using the ID
        If myIEDoc.all.Item("data1").Checked = True Then
        
        Debug.Print (myIEDoc.getElementById("data1").Value)
        
        ElseIf myIEDoc.all.Item("data2").Checked = True Then
        Debug.Print (myIEDoc.getElementById("data2").Value)
        Else
        Debug.Print (myIEDoc.getElementById("data3").Value)
        End If

End Sub

<!doctype html>
<head>
</head>
<body>
<input type="checkbox" id="data1" name="data" value="This is line 1."> This is line 1.<br>
  <input type="checkbox" id="data2"  name="data" value="This is line 2." checked> This is line 2.<br>
  <input type="checkbox" id="data3"  name="data" value="This is line 3."> This is line 3.<br>  
</body>
</html>

在即时窗口中输出。

this

此外,您可以尝试根据需要修改代码。