如何通过VBA单击网站上的按钮?

时间:2019-12-20 17:33:13

标签: javascript excel vba web-scraping

我正在尝试编写VBA代码以从网站上获取一些信息以引入excel。我可以去网站,单击登录按钮,按登录信息输入,然后单击登录按钮,但是我被困在试图关闭主页上的“欢迎弹出窗口”。我应该能够关闭此窗口通过编写代码以单击“欢迎”弹出窗口上的“继续”按钮或“退出”按钮,欢迎弹出窗口。每个按钮的Java代码将发布在我的VBA代码下方。请参见下面的我的VBA代码:

注意:运行该代码目前在excel中没有出现任何错误,但不会关闭欢迎弹出窗口。

Sub ASCElogin()

Dim objIE As Object
Dim UserName As String
Dim Password As String
UserName = Range("G7").Value
Password = Range("G8").Value

'Open Internet Explorer
Set objIE = CreateObject("InternetExplorer.Application")

With objIE
.AddressBar = False
.StatusBar = False
.MenuBar = False
.Toolbar = 0
.Visible = True
.navigate "ASCE 7 Hazard Tool"

Do Until Not objIE.Busy And objIE.readyState = 4
DoEvents
Loop

End With

'Clicking Login Button then goes to 'sign in' page
For Each HTMLInputElement In objIE.document.getElementsByClassName("waves-effect waves-light btn 
blue darken-3")
If HTMLInputElement.className = "waves-effect waves-light btn blue darken-3" Then
HTMLInputElement.Click

Exit For

End If

Next HTMLInputElement

Do Until Not objIE.Busy And objIE.readyState = 4
DoEvents
Loop

'Entering UserName
For Each HTMLInputElement In objIE.document.getElementsByName("ctl00$main$LoginTextBox")
If HTMLInputElement.Name = "ctl00$main$LoginTextBox" Then
HTMLInputElement.Value = UserName

Exit For

End If

Next HTMLInputElement

'Entering Password
For Each HTMLInputElement In objIE.document.getElementsByName("ctl00$main$PasswordTextBox")
If HTMLInputElement.Name = "ctl00$main$PasswordTextBox" Then
HTMLInputElement.Value = Password

Exit For

End If

Next HTMLInputElement

'Clicking Sign-In button and go to main page
For Each HTMLInputElement In objIE.document.getElementsByName("ctl00$main$SubmitButton")
If HTMLInputElement.Name = "ctl00$main$SubmitButton" Then
HTMLInputElement.Click

Exit For

End If

Next HTMLInputElement

Do Until Not objIE.Busy And objIE.readyState = 4
DoEvents
Loop

'Closing Welcome Popup - this currently does not close the welcome popup - but I do not get any 
errors

For Each HTMLInputElement In objIE.document.getElementsByClassName("details-popup-close-icon")
If HTMLInputElement.className = "details-popup-close-icon" Then
HTMLInputElement.Click

Exit For

End If

Next HTMLInputElement

End Sub

Java Code:

For the "Continue" button. Continue button is the last line of code below:

<div class="flex flex-column flex-justify-center align-items-center margin-vertical" data- 
reactid=".0.1.0.4.0.1.1">
    <a class="waves-effect waves-light btn blue darken-3" data- 
    reactid=".0.1.0.4.0.1.1.0">Continue</a>

For the exit button in top right corner of the welcome popup. Exit button is last line of code 
below:

<div class="popup-header blue darken-3 welcome-header" data-reactid=".0.1.0.4.0.0">
    <span data-reactid=".0.1.0.4.0.0.0">ASCE 7 Hazard Tool</span>
    <span class="details-popup-close-icon" data-reactid=".0.1.0.4.0.0.1"></span>

任何帮助将不胜感激!

谢谢

0 个答案:

没有答案