VBA [Internet Explorer] - 按钮提交(登录)

时间:2017-04-27 08:20:30

标签: excel vba excel-vba

我需要一些关于如何通过VBA代码按下按钮的指导。这是在使用下面的代码之前完成的:(请参阅注释“< -----”,看看我被困在哪一行。)

WWW = "http://sezv... ' I can't provide the address since it is on a server.

Set IE = New InternetExplorerMedium
IE.Visible = True
IE.Navigate WWW              
StartDate & EndDate

Do
    If IE.READYSTATE = 4 Then
        Exit Do
    Else
        DoEvents
    End If
Loop

With IE.Document
    .getElementById("Username").Value = "Test"  'Username 
    .getElementById("Password").Value = "Test2" 'Password
    .all("Logga in").Click                      ' <---------
End With

HTML看起来像这样:

<input name="Logga in" class="button right" type="submit" value="Logga in"/>

如何使用看起来像这样的按钮来完成它?:

<button class="REGISTRATION_FORM-Button" style="border-top-color: currentColor; border-right-color: currentColor; border-bottom-color: currentColor; border-left-color: currentColor; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; background-image: url("http://sezvm1091:9090/ais/images/buttonBackground.gif"); background-attachment: scroll; background-repeat: no-repeat; background-position-x: 0px; background-position-y: -50px; background-size: auto; background-origin: padding-box; background-clip: border-box; background-color: transparent;" type="button">

据我所知,这可能与某些脚本有关,如果是,我可以获得一些指导,告诉我如何找到它并将其实现到上面的代码中吗? 我很乐意分享脚本代码,但它是70207行。

3 个答案:

答案 0 :(得分:2)

要找到您可以使用的按钮,例如read.csv您可以在其中指定元素名称和类名。

facultyData <- read.csv("factor_labels.csv")
lapply(facultyData, levels)
#> $Increased.student.engagement
#> [1] "Disagree"                   "Neither agree nor disagree"
#> [3] "Strongly agree"            
#> 
#> $Instructional.time.effectiveness.increased
#> [1] "Neither agree nor disagree" "Strongly agree"            
#> [3] "Strongly disagree"         
#> 
#> $Increased.student.confidence
#> [1] "Neither agree nor disagree" "Strongly agree"            
#> 
#> $Increased.student.performance.in.class.assignments
#> [1] "Disagree"                   "Neither agree nor disagree"
#> [3] "Strongly agree"            
#> 
#> $Increased.learning.of.the.students
#> [1] "Disagree"                   "Neither agree nor disagree"
#> [3] "Strongly agree"            
#> 
#> $Added.unique.learning.activities
#> [1] "Neither agree nor disagree" "Strongly agree"

如果有更多类别相同的类别:

querySelector

然后可以在Dim btn As HTMLButtonElement Set btn = IE.document.querySelector("button[class=REGISTRATION_FORM-Button]") btn.Click 中使用更多属性,例如<button title="Klarmarkera (ctrl+K)" class="ACTION_BUTTON-Button" style="border-top-color" type="button" >Klarmarkera (ctrl+K)</button> <button title="Nytt samtal (ctrl+S)" class="ACTION_BUTTON-Button" style="border-top-color" type="button" >Nytt samtal (ctrl+S)</button>

querySelector

如果有更多的类名,就像这里:

title

Set btn = IE.document.querySelector("button[class=ACTION_BUTTON-Button][title='Nytt samtal (ctrl+S)']") 可能如下所示:

<button class="FancyButton FancyButton-ACTION_FORM-Button" style="border-top-color" type="button">Fancy Form Button</button>

答案 1 :(得分:2)

几周前我几乎问question,而解决方案(有效)就是使用forms().submit所以对于上面的代码来说,它就是

With IE.Document
    .getElementById("Username").Value = "Test"  'Username 
    .getElementById("Password").Value = "Test2" 'Password
    .forms(0).submit ' the index is currently set to 0 but this can be updated as per the DOM
End With

答案 2 :(得分:1)

这应该对你有所帮助。 。

Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Login_2_Website()

Dim oHTML_Element As IHTMLElement
Dim sURL As String

On Error GoTo Err_Clear
sURL = "https://www.google.com/accounts/Login"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True

Do
' Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE

Set HTMLDoc = oBrowser.Document

HTMLDoc.all.Email.Value = "sample@vbadud.com"
HTMLDoc.all.passwd.Value = "*****"

For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next

' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Debug.Assert Err = 0
Err.Clear
Resume Next
End If
End Sub

http://vbadud.blogspot.com/2009/08/how-to-login-to-website-using-vba.html