VBA IE 自动化通过 excel - 获取下拉列表属性值
VBA IE automation through excel - Get drop down list attribute values
我正在尝试将下拉值写入 excel,但是我遇到了一些困难。
这是 HTML 代码:
<div class="select__box" id="wrapBox">
<div class="option" name="US" data-id="United States">
<div class="option__icon"><span class="flag-icon flag-icon-3x flag-icon-us flag-icon-squared option__flag"></span></div>
<div class="option__title">United States</div>
</div>
<div class="option" name="CA" data-id="Canada">
<div class="option__icon"><span class="flag-icon flag-icon-3x flag-icon-ca flag-icon-squared option__flag"></span></div>
<div class="option__title">Canada</div>
</div>
<div class="option" name="GB" data-id="United Kingdom">
<div class="option__icon"><span class="flag-icon flag-icon-3x flag-icon-gb flag-icon-squared option__flag"></span></div>
<div class="option__title">United Kingdom</div>
</div>
<div class="option" name="DO" data-id="Dominican Republic">
<div class="option__icon"><span class="flag-icon flag-icon-3x flag-icon-do flag-icon-squared option__flag"></span></div>
<div class="option__title">Dominican Republic</div>
</div>
<div class="option" name="CN" data-id="China">
<div class="option__icon"><span class="flag-icon flag-icon-3x flag-icon-cn flag-icon-squared option__flag"></span></div>
<div class="option__title">China</div>
</div>
</div>
首先我需要获取国家名称,我可以使用代码
x = objIE.document.getElementsByClassName("option").Length
nextEmptyRow = 1
For i = 0 To x - 1
cnt = objIE.document.getElementsByClassName("option")(i).innerText
ActiveWorkbook.Sheets("Sheet1").Range("A" & nextEmptyRow).Value = cnt
nextEmptyRow = nextEmptyRow + 1
Next i
然后我想获取国家代码(US、CA、GB、DO、CN)
我正在尝试使用类似的代码,但它给了我一个错误,我无法找出问题所在。
For i = 0 To x - 1
cnt2 = objIE.document.getElementByClassName("option")(i).getAttribute("name").innerText
ActiveWorkbook.Sheets("Sheet1").Range("B" & nextEmptyRow2).Value = cnt2
nextEmptyRow2 = nextEmptyRow2 + 1
Next i
解析字符串是一个选项吗?
这里我将HTML存储在Activesheet的单元格A1中用于测试目的:
Option Explicit
Sub test()
Dim parseString As String
parseString = ActiveSheet.Range("A1")
Dim codesArray() As String
codesArray = Split(parseString, "name=""")
Dim i As Long
For i = 1 To UBound(codesArray)
Debug.Print Left$(codesArray(i), InStr(1, codesArray(i), " ") - 2)
Next i
End Sub
HTML 在 sheet(用于测试)
谢谢你的建议,QHarr,但我找到了一个更简单的解决方案。
显然我需要更具体地指出 HTML 代码中的行,我遗漏了 getElementById("wrapBox")
并且 .innerText
或 .Value
是不必要的。
For i = 0 To x - 1
cnt = objIE.document.getElementsByClassName("option")(i).innerText
ActiveWorkbook.Sheets("Sheet1").Range("A" & nextEmptyRow).Value = cnt
cnt2 = objIE.document.getElementById("wrapBox").getElementsByClassName("option")(i).getAttribute("name")
ActiveWorkbook.Sheets("Sheet1").Range("B" & nextEmptyRow).Value = cnt2
nextEmptyRow = nextEmptyRow + 1
Next i
我正在尝试将下拉值写入 excel,但是我遇到了一些困难。
这是 HTML 代码:
<div class="select__box" id="wrapBox">
<div class="option" name="US" data-id="United States">
<div class="option__icon"><span class="flag-icon flag-icon-3x flag-icon-us flag-icon-squared option__flag"></span></div>
<div class="option__title">United States</div>
</div>
<div class="option" name="CA" data-id="Canada">
<div class="option__icon"><span class="flag-icon flag-icon-3x flag-icon-ca flag-icon-squared option__flag"></span></div>
<div class="option__title">Canada</div>
</div>
<div class="option" name="GB" data-id="United Kingdom">
<div class="option__icon"><span class="flag-icon flag-icon-3x flag-icon-gb flag-icon-squared option__flag"></span></div>
<div class="option__title">United Kingdom</div>
</div>
<div class="option" name="DO" data-id="Dominican Republic">
<div class="option__icon"><span class="flag-icon flag-icon-3x flag-icon-do flag-icon-squared option__flag"></span></div>
<div class="option__title">Dominican Republic</div>
</div>
<div class="option" name="CN" data-id="China">
<div class="option__icon"><span class="flag-icon flag-icon-3x flag-icon-cn flag-icon-squared option__flag"></span></div>
<div class="option__title">China</div>
</div>
</div>
首先我需要获取国家名称,我可以使用代码
x = objIE.document.getElementsByClassName("option").Length
nextEmptyRow = 1
For i = 0 To x - 1
cnt = objIE.document.getElementsByClassName("option")(i).innerText
ActiveWorkbook.Sheets("Sheet1").Range("A" & nextEmptyRow).Value = cnt
nextEmptyRow = nextEmptyRow + 1
Next i
然后我想获取国家代码(US、CA、GB、DO、CN)
我正在尝试使用类似的代码,但它给了我一个错误,我无法找出问题所在。
For i = 0 To x - 1
cnt2 = objIE.document.getElementByClassName("option")(i).getAttribute("name").innerText
ActiveWorkbook.Sheets("Sheet1").Range("B" & nextEmptyRow2).Value = cnt2
nextEmptyRow2 = nextEmptyRow2 + 1
Next i
解析字符串是一个选项吗?
这里我将HTML存储在Activesheet的单元格A1中用于测试目的:
Option Explicit
Sub test()
Dim parseString As String
parseString = ActiveSheet.Range("A1")
Dim codesArray() As String
codesArray = Split(parseString, "name=""")
Dim i As Long
For i = 1 To UBound(codesArray)
Debug.Print Left$(codesArray(i), InStr(1, codesArray(i), " ") - 2)
Next i
End Sub
HTML 在 sheet(用于测试)
谢谢你的建议,QHarr,但我找到了一个更简单的解决方案。
显然我需要更具体地指出 HTML 代码中的行,我遗漏了 getElementById("wrapBox")
并且 .innerText
或 .Value
是不必要的。
For i = 0 To x - 1
cnt = objIE.document.getElementsByClassName("option")(i).innerText
ActiveWorkbook.Sheets("Sheet1").Range("A" & nextEmptyRow).Value = cnt
cnt2 = objIE.document.getElementById("wrapBox").getElementsByClassName("option")(i).getAttribute("name")
ActiveWorkbook.Sheets("Sheet1").Range("B" & nextEmptyRow).Value = cnt2
nextEmptyRow = nextEmptyRow + 1
Next i