Home » excel » Using Excel VBA to select onchange value in JavaScript web

Using Excel VBA to select onchange value in JavaScript web

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am very new to VBA, I’ve been tried many times to use Excel VBA to select the value on a website, but it doesn’t work. Below is the code I use.

Actually I don’t know how to get the elements since it has no “id” or “name” tag.

Sub GetFinanceData()

Dim URL As String

URL = "http://stock.finance.sina.com.cn/hkstock/finance/00001.html"

Set myIE = CreateObject("InternetExplorer.Application")

With myIE
.navigate URL
.Visible = True

Do While .Busy = True Or .readyState <> 4
Loop
DoEvents

.Document.parentWindow.execScript "selectData(zero);", "JavaScript"

End With

End Sub

Below is the HTML code, I want to change the option value to “zero” when open the website, can anybody help to check and advise what VBA code I can use to get the value “zero”? Thanks a lot in advance!

enter image description here

	<a name="a1" id="a1"></a>
  	<div class="part02">
   	  <div class="sub01">
       	  <div class="sub01_tt fblue">
           	  <span class=" selected"><a href="#a1" target="_self">重要财务指标</a></span>
              <span class=""><a href="#a2" target="_self">资产负债表</a></span>
              <span class=""><a href="#a3" target="_self">现金流量表</a></span>
              <span class=""><a href="#a4" target="_self">综合损益表</a></span>
              <em class="rt">报表类型:<select class="fgrey" style="width:100px;" interface="getFinanceStandardForjs?symbol=$symbol&financeStanderd=" table="tableGetFinanceStandard" onchange="selectData(this);">
                <option value="all" >全部</option>
                <option value="zero" >年报</option>
                <option value="1" >中报</option>
                <option value="2" >一季报</option>
                <option value="3" >三季报</option>
            </select></em>
        </div>
        <div class="sub01_cc">
            	<div class="sub01_c">
            	                	<table cellspacing="0" cellpadding="0" border="0"  class="tab05">
                    <tbody id="tableGetFinanceStandard">
                     <tr>
How to&Answers:

Indeed, addressing a form element by its HTML name or id is sometimes favorable but in your example, you can use the getElementsByTagName() method to address select tags even though they have no name or id.

Below code will set all select drop downs to “zero”. I tested briefly and it worked fine.

Sub GetFinanceData()
    Dim URL As String
    URL = "http://stock.finance.sina.com.cn/hkstock/finance/00001.html"

    Set myIE = CreateObject("InternetExplorer.Application")
    With myIE
        .navigate URL
        .Visible = True

        Do While .Busy = True Or .readyState <> 4
            Loop
        DoEvents

        Set selectItems = myIE.document.getElementsByTagName("select")
        For Each i In selectItems
            i.Value = "zero"
            i.FireEvent ("onchange")
        Next i
    End With
End Sub

Answer:

CSS selector:

Use a CSS selector to target option[value='zero']

That says option tag having attribute value with value of 'zero'. "[]" means attribute.


CSS query:

css query


VBA:

You apply the selector with querySelector method of document i.e.

To simply select then

ie.document.querySelector("option[value='zero']").Selected = True

If additional event firing needs to be triggered:

ie.document.querySelector("option[value='zero']").FireEvent "onchange"