VBA-从下拉框中提交选定的值

时间:2019-02-19 19:38:43

标签: html vba web-scraping

我正在寻找一种从下拉列表中选择特定选项的方法。我可以突出显示所需的值,但无法激活它。而是,列表中的选项变为蓝色。我也无法再点击其他任何东西。

第二个下拉列表也不会出现,因为它取决于在第一个下拉列表中选择的选项。

有人能指出我正确的方向吗?我假设我必须提交或激活我的选择?但是我一直在尝试多种发现谷歌搜索的东西,但似乎都没有用。

这是我的VBA代码:

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "https://www.2dehands.be/plaats/"


While IE.Busy
    DoEvents
Wend


IE.document.getElementById("manual-option").Click


Set Title = IE.document.getElementById("level1-option")
Title.selectedIndex = 5


IE.Quit
Set IE = Nothing

这是HTML代码:

 <div class="categories-wrapper-wrapper">
  <div class="suggestion-wrapper">
    <div class="custom-radio">
      <input  type="radio" name="suggestion" id="manual-option" class="manual-option" value="manual-option" />
      <label for="manual-option">
        Manueel selecteren
      </label>
    </div>
  </div>

  <div class="categories-wrapper  ">
    <div class="custom-select" data-custom-select>
      <label for="level1-option" class="has-hidden-label">Afdeling</label>
      <span class="btn-select icon-down_thin">Kies afdeling</span>
      <select class="categories-options" data-level="1" name="level1-option" id="level1-option" >
        <option value="">Kies afdeling</option>
        <option value="kunstantiek" 
                data-href="/callback/discovery/kunstantiek/">Antiek &amp; Art</option>
        <option value="audiovideo" 
                data-href="/callback/discovery/audiovideo/">Audio, Video &amp; TV</option>
        <option value="auto-onderd-access" 
                data-href="/callback/discovery/auto-onderd-access/">Auto-onderdelen</option>
        <option value="auto" 
                data-href="/callback/discovery/auto/">Auto's</option>
        <option value="beauty" 
                data-href="/callback/discovery/beauty/">Beauty &amp; Gezondheid</option>
        <option value="auto-bedrijf" 
                data-href="/callback/discovery/auto-bedrijf/">Bestelwagens &amp; Vrachtwagens</option>
        <option value="sieraden" 
                data-href="/callback/discovery/sieraden/">Bijoux &amp; Horloges</option>
        <option value="boek" 
                data-href="/callback/discovery/boek/">Boeken &amp; Strips</option>
        <option value="boten" 
                data-href="/callback/discovery/boten/">Boten</option>
        <option value="brommerscooter" 
                data-href="/callback/discovery/brommerscooter/">Brommers &amp; Scooters</option>
        <option value="kantoor" 
                data-href="/callback/discovery/kantoor/">Business &amp; Industrie</option>
        <option value="kamperen" 
                data-href="/callback/discovery/kamperen/">Caravans &amp; Kamperen</option>
        <option value="cd" 
                data-href="/callback/discovery/cd/">CD &amp; Vinyl</option>
        <option value="computer" 
                data-href="/callback/discovery/computer/">Computer &amp; Game Consoles</option>
        <option value="dieren" 
                data-href="/callback/discovery/dieren/">Dieren &amp; Toebehoren</option>
        <option value="doe-het-zelf-bouw" 
                data-href="/callback/discovery/doe-het-zelf-bouw/">Doe-het-zelf &amp; Bouw</option>
        <option value="witgoed" 
                data-href="/callback/discovery/witgoed/">Elektronische apparatuur</option>
        <option value="fiets" 
                data-href="/callback/discovery/fiets/">Fietsen</option>
        <option value="films-tv-series" 
                data-href="/callback/discovery/films-tv-series/">Films &amp; TV-series</option>
        <option value="foto" 
                data-href="/callback/discovery/foto/">Foto &amp; Camera</option>
        <option value="vrijetijd" 
                data-href="/callback/discovery/vrijetijd/">Hobby</option>
        <option value="meubel" 
                data-href="/callback/discovery/meubel/">Huis &amp; Meubelen</option>
        <option value="huis" 
                data-href="/callback/discovery/huis/">Immo</option>
        <option value="werk" 
                data-href="/callback/discovery/werk/">Jobs &amp; Diensten</option>
        <option value="keuken" 
                data-href="/callback/discovery/keuken/">Keuken</option>
        <option value="kids" 
                data-href="/callback/discovery/kids/">Kinderen &amp; Baby's</option>
        <option value="kleding" 
                data-href="/callback/discovery/kleding/">Kleding &amp; Accessoires</option>
        <option value="landbouw" 
                data-href="/callback/discovery/landbouw/">Landbouw &amp; Tuinbouw</option>
        <option value="motor" 
                data-href="/callback/discovery/motor/">Motoren</option>
        <option value="muziek" 
                data-href="/callback/discovery/muziek/">Muziek</option>
        <option value="overig" 
                data-href="/callback/discovery/overig/">Overig</option>
        <option value="paardensport" 
                data-href="/callback/discovery/paardensport/">Paardensport</option>
        <option value="revalidatie" 
                data-href="/callback/discovery/revalidatie/">Revalidatie &amp; Zorg</option>
        <option value="speelgoed" 
                data-href="/callback/discovery/speelgoed/">Speelgoed &amp; Spelletjes</option>
        <option value="sport" 
                data-href="/callback/discovery/sport/">Sport &amp; Fitness</option>
        <option value="studie" 
                data-href="/callback/discovery/studie/">Studeren</option>
        <option value="telecom" 
                data-href="/callback/discovery/telecom/">Telecommunicatie</option>
        <option value="tuin" 
                data-href="/callback/discovery/tuin/">Tuin</option>
        <option value="vakantie-toerisme" 
                data-href="/callback/discovery/vakantie-toerisme/">Vakantie &amp; Toerisme</option>
        <option value="verzamel" 
                data-href="/callback/discovery/verzamel/">Verzamelen</option>
        <option value="zendamateur" 
                data-href="/callback/discovery/zendamateur/">Zendamateur</option>
      </select>
      <button type="submit" class="category-submit ui-button-secondary " >Selecteer</button>
    </div>
    <div class="custom-select is-disabled" data-custom-select>
      <label for="level2-option" class="has-hidden-label">Rubriek</label>
      <span class="btn-select icon-down_thin">Kies rubriek</span>
      <select class="categories-options" data-level="2" name="level2-option" id="level2-option"  disabled="disabled" >
        <option value="">Kies rubriek</option>
      </select>
      <button type="submit" class="category-submit ui-button-secondary is-hidden"  disabled="disabled" >Selecteer</button>
    </div>

1 个答案:

答案 0 :(得分:0)

Internet Explorer:

查看该select元素的事件侦听器,它监视change事件。如果您添加并触发它有效

Option Explicit
Public Sub MakeSelection()
    Dim ie As New InternetExplorer
    With ie
        .Visible = True
        .Navigate2 "https://www.2dehands.be/plaats/"

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

        .document.querySelector(".icon-radiobutton").Click

        Dim event_onChange As Object
        Set event_onChange = .document.createEvent("HTMLEvents")
        event_onChange.initEvent "change", True, False

        With .document.querySelector("#level1-option")
            .selectedIndex = 2
            .FireEvent "onchange"
            .dispatchEvent event_onChange
        End With
        Stop
        .Quit
    End With
End Sub

Selenium Basic:

我会考虑将selenium basic用于vba。安装后,确保ChromeDriver.exe在selenium文件夹中,然后转到VBE>工具>引用>添加对硒类型库的引用。

Option Explicit
Public Sub MakeSelection()
    Dim d As WebDriver
    Set d = New ChromeDriver
    Const url = "https://www.2dehands.be/plaats/"
    With d
        .Start "Chrome"
        .get url
        .FindElementByCss("button[data-consent-all]").Click
        .FindElementByCss(".icon-radiobutton").Click
        .FindElementById("level1-option").AsSelect.SelectByText "Antiek & Art"
        Stop

        .Quit
    End With
End Sub