我正在尝试访问一个内部网网站,然后从下拉菜单中选择,然后在文本字段中传递一个值,事情是代码正在调整需要选择下拉菜单的位置,而不是从中选择任何选项下拉菜单。
Sub Test()
Dim rng As Range
Set rng = Sheets("sheet1").Range("A1", Sheets("sheet1").Cells.Range("A1").End(xlDown))
Set IE = CreateObject("InternetExplorer.application")
IE.Visible = True
IE.Navigate ("Home URL")
Do
If IE.ReadyState = 4 Then
IE.Visible = True
Exit Do
Else
DoEvents
End If
Loop
IE.Document.forms(0).all("txtUsername").Value = ""
IE.Document.forms(0).all("txtPassword").Value = ""
IE.Document.forms(0).submit
Application.Wait (Now + TimeValue("00:00:04"))
IE.Navigate ("Search URL")
Do
Loop Until IE.ReadyState = 4
Dim z As Object
Set z = IE.Document.getElementById("cboFieldName").selectedIndex = 6
DoEvents
ie.Document.getElementById("txtFieldValue").Select
SendKeys (cell.Value)
DoEvents
ie.Document.getElementById("cmdFind").Click
Next cell
End Sub
和以下网站
<form action="search.asp?Find=1" method="post" onsubmit="return validate(this);">
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tbody><tr>
<td valign="top">
<br><table border="0" cellspacing="0" cellpadding="2" width="100%">
<tbody><tr>
<td colspan="3"><b><font size="3" face="cambria" color="#e60000">Please enter your search criteria:</font></b></td>
</tr>
<tr>
<td width="20%">
<select name="cboFieldName">
<option value="0"></option>
<option value="1">Customer Name</option>
<option value="2">Customer Reference</option>
<option value="3">Site Name</option>
<option value="4">Site City</option>
<option value="5">Site Country</option>
<option value="6">Global Service Reference</option>
<option value="7">Customer Service Reference</option>
答案 0 :(得分:1)
在网页中从下拉列表中选择一个值有很多种方法,它完全取决于网页的构建方式。请尝试以下代码从下拉列表中进行选择
代码1:
IE.Document.all("cboFieldName").selectedIndex = 6
IE.Document.all("cboFieldName").FireEvent ("onchange")
对于某些下拉菜单,您可以直接传递值
代码2:
Set z = IE.Document.all("cboFieldName")
z.Value = "Global Service Reference" ' update the option name
更新:
Dim workForm As HTMLFormElement
Set workForm = ie2.Document.forms("form name") 'update your exact form name
workForm.all("cboFieldName").selectedIndex = 6
workForm.all("cboFieldName").FireEvent ("onchange")
为了更好地为您提供帮助,请在与下拉按钮相关联的网站中提供代码。
谢谢