首页 > 解决方案 > Excel VBA IE HTML网页中的下拉列表

element

问题描述

我正在努力在网站上自动填写表格,但无法通过这个字段,该字段是元素内的下拉列表。

HTML 代码如下所示,当我在网页上手动选择值时,又添加了一个(显示在 HTML 代码之后)。

<div class="county-select-input">
    <div class=" css-2b097c-container" id="OES_area">
    <div class=" css-yk16xz-control">
    <div class="css-1hwfws3">
    <div class=" css-1wa3eu0-placeholder">Type search term here</div>
    <div class="css-1g6gooi">
    <div style="display: inline-block;"><style>input#react-select-3-input::-ms-clear {display: none;}</style><input tabindex="0" id="react-select-3-input" style="background: 0px; padding: 0px; outline: 0px; border: 0px currentColor; border-image: none; width: 19px; color: inherit; font-size: inherit; box-sizing: content-box; opacity: 1;" spellcheck="false" aria-autocomplete="list" type="text" value="" autoComplete="off" autoCapitalize="none" autoCorrect="off">
    <div style="left: 0px; top: 0px; height: 0px; text-transform: none; letter-spacing: normal; overflow: scroll; font-family: Source Sans Pro,Helvetica Neue,Helvetica,Roboto,Arial,sans-serif; font-size: 16px; font-style: normal; font-weight: 400; white-space: pre; visibility: hidden; position: absolute;"></div></div></div></div>
    <div class="css-1wy0on6"><span class=" css-1okebmr-indicatorSeparator"></span>
    <div class=" css-tlfecz-indicatorContainer" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" class="css-19bqh2r" aria-hidden="true" viewBox="0 0 20 20" focusable="false" width="20" height="20"><path d="M 4.516 7.548 c 0.436 -0.446 1.043 -0.481 1.576 0 l 3.908 3.747 l 3.908 -3.747 c 0.533 -0.481 1.141 -0.446 1.574 0 c 0.436 0.445 0.408 1.197 0 1.615 c -0.406 0.418 -4.695 4.502 -4.695 4.502 c -0.217 0.223 -0.502 0.335 -0.787 0.335 s -0.57 -0.112 -0.789 -0.335 c 0 0 -4.287 -4.084 -4.695 -4.502 s -0.436 -1.17 0 -1.615 Z" /></svg></div></div></div><input name="OES_area" type="hidden" value=""></div></div>
当我手动选择值时,此 div 网页会添加此新内容。 <div class="css-1uccc91-singleValue">Baldwin County - Daphne-Fairhope-Foley, AL</div>

这就是字段看起来像网页的样子

我尝试了多种方法来解决这个问题,但没有结果..发布了我尝试过的两种方法。 选项1:

Dim inputfield_County As Object
Set inputfield_County = ie.Document.getElementsByClassName("css-1g6gooi")(0).getElementsByTagName("div")(0).getElementsByTagName("input")(0)
MsgBox inputfield_County
If inputfield.Length > 0 Then
    inputfield_County(0).Focus
    inputfield_County(0).Click
    inputfield_County(0).Value = "Butler County - Southeast Alabama nonmetropolitan area"
    Call iewait(ie)
End If

选项 2:

 Dim HTMLdoc_County_00 As htmlDocument
    Dim selectElement_County_00 As HTMLSelectElement
    Dim evtChange_County_00 As Object
    Set HTMLdoc_County_00 = ie.Document
    Set evtChange_County_00 = HTMLdoc_County_00.createEvent("HTMLEvents")
    evtChange_County_00.initEvent "change", True, False
    Set selectElement_County_00 = HTMLdoc_County_00.getElementsByClassName("css-19bqh2r")(0)
    selectElement_County_00.innerText = "Fayette County - Northwest Alabama nonmetropolitan area" 'London1
    'Set selectElement_County_00 = HTMLdoc_County_00.getElementsByClassName("css-1g6gooi")(0).getElementsByTagName("div")(0).getElementsByTagName("input")(0)
    selectElement_County_00.Value = "Fayette County - Northwest Alabama nonmetropolitan area" 'London1
    'Set selectElement_County_00 = HTMLdoc_County_00.getElementsByClassName(" css-1wa3eu0-placeholder")(0)
    ' css-1wa3eu0-placeholder/ css-yk16xz-control/
    MsgBox selectElement_County_00
    'selectElement_County_00.Focus
    'selectElement_County_00.Click
'    selectElement_County_00.Value = "Fayette County - Northwest Alabama nonmetropolitan area" 'London1
'    'MsgBox selectElement_County_00.innerText
'    selectElement_County_00.dispatchEvent evtChange
    Call iewait(ie)

非常感谢任何帮助或指导...

标签: javascripthtmlexcelvbahtml.dropdownlistfor

解决方案


推荐阅读