我目前正在开发一个脚本来打开一个网站,在下拉列表中选择一个值并进行搜索,我在下拉列表中选择了一个值时出现问题,下面是我的vbs代码:
Dim IE
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = 1
IE.navigate "https://website.com/form.aspx"
Do
WScript.Sleep 10
Loop While IE.ReadyState <4 And IE.Busy
'Button to access form
Set SLTOI = IE.document.getElementByID("ctl00_m_g_4e7438b0_6aba_44b0_a0a1_f49486f81665_FormControl0_V1_I1_S1_I1_B1")
SLTOI.click
'Dropdownlist element
Set SPCI = IE.document.getElementByID("ctl00_m_g_4e7438b0_6aba_44b0_a0a1_f49486f81665_FormControl0_V1_I1_S2_I2_D7")
For Each opt In SPCI.options
MsgBox opt.value
If opt.value = "ION CISCO" Then
opt.Selected = True
Else
opt.Selected = False
End If
Next
下拉列表的来源如下(使用开发人员选项来检查IE中的元素)
<SELECT tabIndex=0 title="" class="v_utCmzxtdUQZu9e3j_0 n_utCmzxtdUQZu9e3j_0 bb_utCmzxtdUQZu9e3j_0 d3_utCmzxtdUQZu9e3j_0" id=ctl00_m_g_4e7438b0_6aba_44b0_a0a1_f49486f81665_FormControl0_V1_I1_S2_I2_D7 style="DIRECTION: ltr" onfocus="return (DropDownList.OnFocus(this, event));" onblur="return (DropDownList.OnBlur(this, event));" onchange="return (DropDownList.OnChange(this, event));" OriginalId="V1_I1_S2_I2_D7" FormId="ctl00_m_g_4e7438b0_6aba_44b0_a0a1_f49486f81665_FormControl0" ViewDataNode="12" direction="ltr" wrapped="true" ScriptClass="DropDownList"><OPTION value=""></OPTION><OPTION value="ION CISCO" selected>ION CISCO</OPTION></SELECT>
代码正确执行,但未在下拉列表字段中选择该值(在For中添加一个Msgbox以查看代码是否已执行,但它根本不输入For),如果我从列表中手动选择一个值然后尝试选择另一个值,该列表只有我之前选择的值,除非我单击一个清除按钮,列表没有再次填充,似乎正在运行一个脚本来填充列表。
关于我如何使我的代码工作的任何想法都试图使用&#34; FireEvent(&#34; onchange&#34;)&#34;做了选择但没有工作。
任何帮助将不胜感激!
答案 0 :(得分:0)
这个怎么样?此外,您应该将IE声明为Object而不是变体
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = 1
IE.navigate "https://website.com/form.aspx"
Do
WScript.Sleep 10
Loop While IE.ReadyState <4 And IE.Busy
'Button to access form
Set SLTOI = IE.document.getElementByID("ctl00_m_g_4e7438b0_6aba_44b0_a0a1_f49486f81665_FormControl0_V1_I1_S1_I1_B1")
SLTOI.click
'Dropdownlist element
Set SPCI = IE.document.getElementByID("ctl00_m_g_4e7438b0_6aba_44b0_a0a1_f49486f81665_FormControl0_V1_I1_S2_I2_D7")
Dim SelectThis as String
SelectThis = "ION CISCO"
SPCI.Value = SelectThis