Excel选项标签中的Excel VBA更改选项 [英] Excel VBA change option in the HTML select tag
问题描述
最近,我发布了 vbscript捕获文本的问题HTML选择选项标签。这在Internet Explorer平台上给了我极大的帮助。但是,我有一个网站更复杂一些的新项目。该网站有多个嵌套表格,格式化为 select
tag)之外的大多数元素中获取和设置数据。我们可以从下拉菜单中获取文本,但我们无法更改此值。相反,所有选项都被清除了,整个下拉菜单都是空白的。
这是网站代码的修订版本,因为涉及的代码更多,但只显示相关的代码:
< html xmlns =http://www.w3.org/1999/ XHTML>
< head> ...< / head>
< body>
< form id =form1action =AssignOFS.aspxmethod =post>
< div class =aspNetHidden> ...< / div>
< div class =aspNetHidden> ...< / div>
< table width =100%align =centercellspacing =0>
< tbody>
< tr>
< td>
< table width =100%align =centercellspacing =0> ...< / table>
< / td>
< / tr>
< tr>
< td>
< table width =100%align =centercellspacing =0>
< tbody>
< tr> ...< / tr>
< tr> ...< / tr>
< tr class =tr_data> ...< / tr>
< tr>
< td align =center>
< div id =pnlBtn1> ...< / div>
< div id =pnlView>
< table width =100%align =centercellspacing =0>
< tbody>
< tr> ...< / tr>
< tr> ...< / tr>
< tr class =tr_data>
< td align =center> ...< / td>
< td valign =top> ...< / td>
< td valign =top> ...< / td>
< td valign =top> ...< / td>
< td nowrap =valign =top> ...< / td>
< td align =left> ...< / td>
< td align =center> ...< / td>
< td align =center>
< option value => - 选择用户 - < / option>
< option value =0123>艾米琼斯(000456321)< / option>
< option value =0432> Brian Wyatt(000745632)< / option>
< option value =0345> Carl Lister(000874563)< / option>
< option value =0654> Daniel Michaels(000987456)< / option>
< option value =0567> Elizabeth Sweeny(001456321)< / option>
< option value =0876> Fran Tarris(001745632)< / option>
< option value =0789> Gail McMurphy(001874563)< / option>
< option value =1098> Hannah Barisce(001987456)< / option>
< / select>
< / td>
< / tr>
< / tbody>
< / table
< / div>
< / td>
< / tr>
< / tbody>
< / table>
< / td>
< / tr>
< / tbody>
< / table>
< / form>
< / body>
< / html>
为了能够从下拉菜单中获取文本,我们使用了在查找包含下拉列表的表中的节点后找到类
名称 txt_input1
。此代码会在下拉列表中给我们提供整个列表:
objIE.Document.getElementsByTagName(table)(0).getElementsByTagName( td)。Item(64).GetElementsByClassName(txt_input1)(0).innerText
选择
从 - 选择用户 - 下拉列表中的其中一个名称,使用以下代码:
$ b (0).getElementsByTagName(td)。Item(64).GetElementsByClassName(txt_input1)(0).innerText =$ b
objIE.Document.getElementsByTagName(table Carl Lister
这导致空白下拉。如果我将结尾更改为 .Value
,它只会删除显示的--Select User--。它仍然不会改变Carl Lister的价值。在嵌套表中选择选择
下拉列表中选择选项
时是否缺少某些东西? / p>
感谢@freginold,为您提供所有帮助。我和我的团队想出了一个扩展了我们已经开发的解决方案的解决方案。
tdNode = 64
i = 0
对于每个objOption在objIE.Document.GetElementsByTagName(table)(0).GetElementsByTagName(td)(tdNode).GetElementsByClassName(txt_input1)(0).Options
Q(i)= objOption.Text& - & objOption.Value
QNm = QNm& | &安培; Q(i)
i = i + 1
如果objOption.Selected Then
strWQ = objOption.Text
End If
如果i> 53 Then
MsgBoxstuck
SetEverythingToNothing
End If
Next
QNm = QNm& | =& strWQ
a = 1
MyArray = Split(QNm,|)
对于= LBound(MyArray)至UBound(MyArray)
If InStr(MyArray(a),UserName)Then
strWQ = MyArray(a)
End If
Next
SelQ = Mid(strWQ,InStr(strWQ, - )+ 1,Len(strWQ) - InStr(strWQ, - ))
对于每个objOption在objIE.Document.GetElementsByTagName(table)(0).GetElementsByTagName(td )(tdNode).GetElementsByClassName(txt_input1)(0).Options
如果objOption.Value = SelQ Then
objOption.Selected = True
objIE.Document.GetElementsByTagName(table )(0).GetElementsByTagName(td)(tdNode).GetElementsByClassName(txt_input1)(0).OnChange
Else
objOption.Selected = False
End If
下一个
Recently, I posted the question vbscript capture text in the HTML select option tag. This helped me tremendously on an Internet Explorer platform. However, I have a new project with a bit more complex of a web site. The web site has multiple nested tables that are formatted to hold form
elements. My team and I have figured out how to get and set data within most of the elements except the drop down box (select
tag). We are able to get the text from the drop down, but we can't change the value. Instead, all the options are wiped clear and the whole drop down is blank.
Here's a revised version of the code for the web site as there is much more code involved, but only the pertinent code is shown:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>...</head>
<body>
<form id="form1" action="AssignOFS.aspx" method="post">
<div class="aspNetHidden">...</div>
<div class="aspNetHidden">...</div>
<table width="100%" align="center" cellspacing="0">
<tbody>
<tr>
<td>
<table width="100%" align="center" cellspacing="0">...</table>
</td>
</tr>
<tr>
<td>
<table width="100%" align="center" cellspacing="0">
<tbody>
<tr>...</tr>
<tr>...</tr>
<tr class="tr_data">...</tr>
<tr>
<td align="center">
<div id="pnlBtn1">...</div>
<div id="pnlView">
<table width="100%" align="center" cellspacing="0">
<tbody>
<tr>...</tr>
<tr>...</tr>
<tr class="tr_data">
<td align="center">...</td>
<td valign="top">...</td>
<td valign="top">...</td>
<td valign="top">...</td>
<td nowrap="" valign="top">...</td>
<td align="left">...</td>
<td align="center">...</td>
<td align="center">
<select name="user" class="txt_input1" id="user" onchange="javascript:addUser(227);">
<option value="">--Select User--</option>
<option value="0123">Amy Jones (000456321)</option>
<option value="0432">Brian Wyatt (000745632)</option>
<option value="0345">Carl Lister (000874563)</option>
<option value="0654">Daniel Michaels (000987456)</option>
<option value="0567">Elizabeth Sweeny (001456321)</option>
<option value="0876">Fran Tarris (001745632)</option>
<option value="0789">Gail McMurphy (001874563)</option>
<option value="1098">Hannah Barisce (001987456)</option>
</select>
</td>
</tr>
</tbody>
</table
</div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
To be able to get the text from the drop down, we used the class
name txt_input1
after locating the node within the table that holds the drop down. This code will give us the entire list within the drop down:
objIE.Document.getElementsByTagName("table")(0).getElementsByTagName("td").Item(64).GetElementsByClassName("txt_input1")(0).innerText
I've tried changing the value of the select
drop down from "--Select User--" to one of the names in the list with the following code:
objIE.Document.getElementsByTagName("table")(0).getElementsByTagName("td").Item(64).GetElementsByClassName("txt_input1")(0).innerText = "Carl Lister"
This resulted in the blank drop down. If I changed the ending to .Value
, it only removes "--Select User--" from being shown. It still doesn't change the value to "Carl Lister". Is there something we're missing when it comes to selecting an option
in a select
drop down within nested tables?
Thanks, @freginold, for all your help. My team and I came up with a solution that expands upon our already developed solutions.
tdNode = 64
i = 0
For Each objOption In objIE.Document.GetElementsByTagName("table")(0).GetElementsByTagName("td")(tdNode).GetElementsByClassName("txt_input1")(0).Options
Q(i) = objOption.Text & "-" & objOption.Value
QNm = QNm & "|" & Q(i)
i = i + 1
If objOption.Selected Then
strWQ = objOption.Text
End If
If i > 53 Then
MsgBox "stuck"
SetEverythingToNothing
End If
Next
QNm = QNm & "|=" & strWQ
a = 1
MyArray = Split(QNm, "|")
For a = LBound(MyArray) To UBound(MyArray)
If InStr(MyArray(a), UserName) Then
strWQ = MyArray(a)
End If
Next
SelQ = Mid(strWQ, InStr(strWQ, "-") + 1, Len(strWQ) - InStr(strWQ, "-"))
For Each objOption In objIE.Document.GetElementsByTagName("table")(0).GetElementsByTagName("td")(tdNode).GetElementsByClassName("txt_input1")(0).Options
If objOption.Value = SelQ Then
objOption.Selected = True
objIE.Document.GetElementsByTagName("table")(0).GetElementsByTagName("td")(tdNode).GetElementsByClassName("txt_input1")(0).OnChange
Else
objOption.Selected = False
End If
Next
这篇关于Excel选项标签中的Excel VBA更改选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!