如何在网站上设置ComboBox的值?

时间:2019-05-23 13:22:51

标签: excel vba web-scraping

我一直在尝试编写一些登录到网站的代码,尽管我可以设置“登录”或“密码”的值,但无法设置“ empresa”字段的值,因为它是一个组合框。

登录页面在此链接上的图像中:https://i.stack.imgur.com/3bk6B.png

当我检查网站上的对象时,“ Empresa”字段会显示以下信息:

<div id="cmbProject_cmb" class="RadComboBox RadComboBox_" style="border-style:None;font-size:18px;width:100%;white-space:normal;background-color:transparent;">

<table summary="combobox" style="border-width:0;border-collapse:collapse;width:100%" class="rcbFocused">

<tbody><tr class="rcbReadOnly">

<td class="rcbInputCell rcbInputCellLeft" style="width:100%;"><input name="cmbProject$cmb" type="text" class="rcbInput radPreventDecorate" id="cmbProject_cmb_Input" value="Empresa" readonly="readonly" style="color:Black;font-size:18px;" autocomplete="off"></td><td class="rcbArrowCell rcbArrowCellRight"><a id="cmbProject_cmb_Arrow" style="overflow: hidden;display: block;position: relative;outline: none;">select</a></td>

</tr>

</tbody></table><input id="cmbProject_cmb_ClientState" name="cmbProject_cmb_ClientState" type="hidden" autocomplete="off" value="{"logEntries":[],"value":"WTM - PROD|1","text&quot;:"WTM - PROD","enabled":true,"checkedIndices":[],"checkedItemsTextOverflows":false}">

</div>

我一直在使用的代码如下

Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer

Sub Login()

Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
   sURL = "https://prodweb-votorantim.mc1.com.br/WTM/aspx/Login.aspx"
Set oBrowser = New InternetExplorer
   oBrowser.Silent = True
   oBrowser.timeout = 60
   oBrowser.navigate sURL
   oBrowser.Visible = True

Do
Loop Until oBrowser.readyState = READYSTATE_COMPLETE

Set HTMLDoc = oBrowser.document
  HTMLDoc.all.txtUserName_txt.Value = "login"
  HTMLDoc.all.txtPassword_txtPass.Value = "password"
  HTMLDoc.all.cmbProject.Value = "WTM - PROD" 

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

Next
Err_Clear:

Resume Next
End Sub

1 个答案:

答案 0 :(得分:0)

您可以使用javascript设置值

Option Explicit
'VBE > Tools > References: Microsoft Internet Controls
Public Sub Login()
    Dim ie As New InternetExplorer
    With ie
        .Visible = True
        .Navigate2 "https://prodweb-votorantim.mc1.com.br/WTM/aspx/Login.aspx"

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document
            .querySelector("#txtUserName_txt").Value = "bob.builder@web.net"
            .querySelector("#txtPassword_txtPass").Value = "topSecret"
            .parentWindow.execScript "document.querySelector('#cmbProject_cmb_Input').value = 'WTM-PROD';"
            .querySelector("#btnLogin_input").Click
        End With

        Stop ' <delete me later
        .Quit
    End With
End Sub