Excel VBA导航的html元素

时间:2015-11-20 16:05:05

标签: vba excel-vba web-scraping excel

我正在使用VBA-excel导航到网站的代码。登录网站后,我无法与页面中的任何html元素进行交互。我需要能够单击某些复选框并在字段中输入日期范围。这是我的代码。当我打开一个新窗口但它无法让它在原始窗口中工作时,它可以工作。如果可能的话,我希望在一个窗口中完成此过程。任何反馈都非常感谢。

Private Sub CommandButton1_Click()
Dim yesterday As String
yesterday = Date - 1
Dim RowCount As Integer
Dim x As Integer
Dim nik As Integer

'Opens Internet Explorer
Dim Shellwins As SHDocVw.ShellWindows
Dim Shellie As SHDocVw.InternetExplorer
Set Shellwins = New SHDocVw.ShellWindows
Dim ie1 As InternetExplorer


Strurl = "father.com"
Set ie1 = New InternetExplorer
ie1.Visible = True
ie1.navigate Strurl
Do While ie1.readyState <> 4 And ie1.Busy
    DoEvents
Loop

Set htmldoc = ie1.document

For Each htmlel In htmldoc.getElementsByTagName("input")
'MsgBox htmlel.Name
    If htmlel.ID = "ctl00_Body_txtEmailAddress" Then
        htmlel.Value = "dad@dad.com"
    End If
Next

Set htmldoc = ie1.document
For Each htmlel In htmldoc.getElementsByTagName("input")
    'MsgBox htmlel.Name
    If htmlel.ID = "ctl00_Body_txtPassword" Then
        htmlel.Value = "mom"
    End If
Next

Set htmldoc = ie1.document
For Each htmlel In htmldoc.getElementsByTagName("input")
    If htmlel.ID = "ctl00_Body_btnLogon" Then
        htmlel.Click
    End If
Next

Strurl = "url.com"

Set ie2 = New InternetExplorer
ie2.Visible = True
ie2.navigate Strurl

ie1.Quit

Do While ie2.readyState <> 4 And ie2.Busy
    DoEvents
Loop
Set htmldoc = ie2.document
For Each htmlel In htmldoc.getElementsByTagName("input")

    If htmlel.ID = "ctl00_Body_txtStartDate" Then
        htmlel.Value = yesterday
    End If
Next

Set htmldoc = ie2.document
For Each htmlel In htmldoc.getElementsByTagName("input")
    If htmlel.ID = "ctl00_Body_txtEndDate" Then
        htmlel.Value = yesterday
    End If
Next

0 个答案:

没有答案