2016-06-07 126 views
0

enter image description here 我的代码:如何在网页上选择下拉菜单中的值:VBA

Sub login() 
    Dim IE As Object 
    Dim HTMLDoc As Object, HTMLDoc2 As Object 
    Dim objCollection As Object 

    Const navOpenInNewTab = &H800 
    Set IE = CreateObject("InternetExplorer.Application") 
    IE.Visible = True 
    IE.Navigate "https:/com/1/19/login.esp" 

    Do While IE.Busy Or IE.ReadyState <> 4: Loop 

    Set HTMLDoc = IE.Document 

    With HTMLDoc 
    HTMLDoc.getElementById("USERNAME").Value = "xxxx" 'Entering credential 
    HTMLDoc.getElementById("PASSWORD").Value = "yyyyy" 
    End With 


    Set objCollection = IE.Document.getElementById("loginbutton") 
    objCollection.Click 

'Second webpage 

    Do While IE.Busy Or IE.ReadyState <> 4: Loop ' opening the second webpage 

    Set HTMLDoc2 = IE.Document 
    With HTMLDoc2 
    **HTMLDoc2.getElementById("DEPARTMENTID").selectedindex = 1 'Drop down menu 
    HTMLDoc2.getElementById("DEPARTMENTID").FireEvent ("onchange")** 
    End With 

    Set objCollection = IE.Document.getElementById("loginbutton") 
    objCollection.Click 

End Sub 

Q)代码更改做我做的选择Dwell_DF选项值1567? 上面的代码给出了运行时错误'424':需要对象。

HTMLDoc2.getElementById("DEPARTMENTID").selectedindex = 1 'Drop down menu 
      HTMLDoc2.getElementById("DEPARTMENTID").FireEvent ("onchange") 

上面的行给出错误。

在第一个网页中,我填写登录凭证,然后在下一页中粘贴该图像的图像。在这里我想更改下拉菜单中的值。

+0

什么行会抛出错误? – OpiesDad

回答

0

试试看。值“1567”对应于InnerText“Dwell_DF”。

With HTMLDoc2 
    .getElementById("DEPARTMENTID").Focus 
    .getElementById("DEPARTMENTID").Value = "1567" 'You can also loop to find the text of the Option 
    .getElementById("DEPARTMENTID").FireEvent ("onchange") 
End With 
0

它应该是这样的。

Sub passValueToComboBox1() 
    Dim ie As Object 
    Dim oHTML_Element As IHTMLElement 

    Set ie = CreateObject("InternetExplorer.Application") 
    ie.Visible = True 
    ie.navigate "http://your_URL_here.php" 
    While ie.Busy Or ie.readyState <> 4: DoEvents: Wend 

    Set oHTML_Element = ie.document.getElementsByName("selectedReportClass")(0) 
    If Not oHTML_Element Is Nothing Then oHTML_Element.Value = "FUBU7" 

    For Each oHTML_Element In ie.document.getElementsByTagName("input") 
     If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For 
    Next 
End Sub 

查看下面的链接,了解如何以编程方式与网站进行交互的其他想法。

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

相关问题