从下拉列表中选择给定值,然后单击添加按钮

问题描述 投票:1回答:1

我可以登录我的网站,然后导航到一个网页,我需要从下拉框中选择一个值,然后点击添加按钮。 (使用vba)。从下拉列表中选择给定值,然后单击添加按钮。

我无法做到这一点,我已经尝试但无法做到这一点。

我的下拉列表html代码是:

<select id="input_ifxlist_opts"><option value="43.66.18.70>11">SAL-EC-S1&gt;sp_wan</option>

    <option value="43.72.38.250>11">SDT-EC-S1&gt;sp_wan</option>
    <option value="43.95.88.9>3">SISC-CE2&gt;Gi0/2</option>
    <option value="43.95.88.5>3">SISC-CE1&gt;Gi0/2</option>
    <option value="43.88.32.237>11">SID-EC-S1&gt;sp_wan</option>
    <option value="43.95.74.54>2">SOEM_PG-CE1V.virtela.net&gt;Gi0/0/1</option>
    <option value="43.95.66.1>2">SAL-CE1&gt;Gi0/1</option>
    <option value="43.76.42.10>2">SEK-CE1V&gt;Gi0/0</option>
    <option value="43.95.94.5>2">SEV-CE2&gt;Gi0/1</option>
    <option value="43.95.78.9>2">SI-CE2&gt;Gi0/1</option>
    <option value="43.95.88.13>3">SID-CE1&gt;Gi0/1</option>
    <option value="43.95.76.5>1">SOK-CE1&gt;Gi0/0</option>
    <option value="43.95.86.9>37">SOMEA-CE1&gt;Gi0/1.102</option>
    <option value="43.95.92.9>2">SPH-CE1&gt;Gi0/0/1</option>
    <option value="43.95.70.2>2">STWN-CE1V&gt;Gi0/1</option>
    <option value="43.95.74.2>3">SOEM_KL-CE3&gt;Gi0/1</option>
    <option value="43.95.74.62>2">SOEM_KL-CE1V.virtela.net&gt;Gi0/0/1</option>
    <option value="43.95.74.46>2">SOMAS-CE1V&gt;Gi0/0/1</option>
    <option value="43.95.72.33>2">SDT-CE1.virtela.net&gt;Gi0/0/1</option>
    <option value="43.95.72.45>2">SOTHAI-CE&gt;Gi0/0/1</option>
    <option value="43.95.72.41>2">STT-CE1V.virtela.net&gt;Gi0/0/1</option>
    <option value="43.95.72.37>2">STTB-CE1.virtela.net&gt;Gi0/0/1</option>
    <option value="43.74.61.6>11">SOEM-PG-EC-S1&gt;sp_wan</option>
    <option value="43.95.92.2>2">SPHWNS-CE&gt;Gi0/0/1</option>
    <option value="43.95.65.1>4">GDC-CE1&gt;Gi0/2</option>
    <option value="43.72.61.5>11">SOTHAI-EC-S1&gt;sp_wan</option>
    <option value="146.215.74.110>3">IBP-CE1&gt;Gi0/1</option>
    <option value="43.95.86.9>2">SOMEA-CE1&gt;Gi0/1</option>
    <option value="43.95.88.5>2">SISC-CE1&gt;Gi0/1</option>
    <option value="43.95.88.9>2">SISC-CE2&gt;Gi0/1</option>
</select>

我的添加按钮html代码是:

<button type="button" onclick="addToList(document.forms['queryform'].input_ifxlist,document.getElementById('input_ifxlist_opts'))">Add</button>

Sub login_page()
    Dim ieApp As SHDocVw.InternetExplorer
    Dim iedoc As MSHTML.HTMLDocument
    'Dim ieApp As InternetExplorer
    'Dim ieDoc As Object
    'Dim ieTable As Object
    'Dim clip As DataObject

    Set ieApp = New SHDocVw.InternetExplorer

    ieApp.Visible = True

    ieApp.navigate "http:"
    Do While ieApp.Busy: DoEvents: Loop
    Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop

    Set iedoc = ieApp.document

    'fill in the login form – View Source from your browser to get the control names
    With iedoc.forms(0)
    .user.Value = "id"
    .Password.Value = "Pass"
    .submit

    End With

    ieApp.navigate "http:"
    Do While ieApp.Busy: DoEvents: Loop 

    ieApp.navigate "http:"
    Do While ieApp.Busy: DoEvents: Loop  

    ieApp.navigate "http:"
    Do While ieApp.Busy: DoEvents: Loop


    ieApp.navigate "http:"
    Do While ieApp.Busy: DoEvents: Loop

    ieApp.navigate "http:"
    Do While ieApp.Busy: DoEvents: Loop 

    ieApp.navigate "http:"
    Do While ieApp.Busy: DoEvents: Loop

    ieApp.navigate "http:"
    Do While ieApp.Busy: DoEvents: Loop

    ieApp.navigate "http:"
    Do While ieApp.Busy: DoEvents: Loop
End Sub
html vba web-scraping
1个回答
0
投票

您可以使用attribute = value选择器选择特定值,例如

ieApp.document.querySelector("[value='43.72.38.250>11']").Selected = True

您还可以在select元素上使用index

ieApp.document.querySelector("#input_ifxlist_opts").SelectedIndex = 2

您可以使用带有^运算符的开头单击带有attribute = value选择器的按钮

ieApp.document.querySelector("[onclick^=addToList]").click

您可以使用更紧凑的语法进行正确的页面等待:

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

你也可以尝试:

ieApp.document.parentWindow.ExecScript "document.forms ['queryform'].input_ifxlist.value='43.96.83.197>11'"
ieApp.document.querySelector("[onclick^=addToList]").click
© www.soinside.com 2019 - 2024. All rights reserved.