заставить программу взаимодействовать с чем-то, показанным на веб-странице - PullRequest
0 голосов
/ 01 октября 2018

Я хочу построить программу, которая взаимодействует с веб-страницей, например:

У меня есть 3 строки A, B и C, показанные в браузере, когда строка A стала зеленой, я хочу получить какое-то значениев переменной.и так далее с другими строками.Я не хочу открывать страницу в своем коде, страница уже открыта в проводнике Windows, и я хочу, чтобы мой код взаимодействовал с ней там, где она есть.

Надеюсь, я четко объяснил свой вопрос.

1 Ответ

0 голосов
/ 02 октября 2018

Я пытаюсь получить доступ к уже открытой веб-странице, используя приведенный ниже код 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>

Вывод в непосредственном окне.

enter image description here

Далее, вы можете попытаться изменить код каксогласно вашему требованию.

...