我有一个vba脚本,可以自动浏览ups.com。它确实很好,直到它有一个不同的URL的子窗口。在那个子窗口是一个简单的窗体,但是当脚本提交表单时,它只刷新屏幕或出现错误,说明该站点不可用。子窗口的URL是
https://wwwapps.ups.com/webClaims/address
并且该窗口上的相关HTML
是
<script language="javascript" type="text/JavaScript">
function updateReturn()
{
"use strict" ;
opener.document.reportform.raddress.value = "true";
opener.document.reportform.submit();
top.close();
}
function updateDeliver()
{
"use strict" ;
opener.document.reportform.daddress.value = "true";
opener.document.reportform.submit();
top.close();
}
function updateContact()
{
"use strict" ;
opener.document.reportform.caddress.value = "true";
opener.document.reportform.submit();
top.close();
}
function updateOther()
{
"use strict" ;
opener.document.reportform.oaddress.value = "true";
opener.document.reportform.submit();
top.close();
}
function submitRefresh()
{
"use strict" ;
this.document.address.refresh.value = "true";
document.address.submit();
}
</script>
<div id="main">
<form name="address" action="/webClaims/address" method="post">
<input type="hidden" name="loc" value="en_US">
<input type="hidden" name="addrtype" value="f">
<input type="hidden" name="refresh" value="">
<table border="0" cellpadding="0" cellspacing="0" width="566">
<tr>
<td valign="top" width="422">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td class="pi-mA1-t" valign="top">
<table border="0" cellpadding="0" cellspacing="0" width="422">
<tr>
<td class="pi-m-txt-title">
Edit Shipped From Address
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="pi-mA1-c-t-spc"><img alt="" border="0" height="1" src="/img/1.gif" width="1"></td>
</tr>
<tr>
<td class="pi-mA1-c" valign="top">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td>
<div class="pi-m-pad">
Please edit the address below. Required fields are shown in <span class="pi-h4">bold</span>.<br>
<br>
<span class="pi-h4">Company Name:</span>
<br>
<input class="modTxtMedium" name="companyName" maxlength="50" size="19" type="text" value="">
<br><br>
<span class="pi-h4">Name:</span>
<br>
<input class="modTxtMedium" name="name" maxlength="50" size="19" type="text" value="">
<br><br />
<span class="pi-h4">Country:</span>
<br>
<select name="country" class="modSelMedium" onChange="javascript:submitRefresh()" >
<option SELECTED value="US">United States</option>
<option value="CA">Canada</option>
</select>
<br><br>
<span class="pi-h4">Address Line 1:</span>
<br>
<input class="modTxtMedium" name="address1" maxlength="50" size="19" type="text" value="">
<br><br>
Address Line 2:
<br>
<input class="modTxtMedium" name="address2" maxlength="50" size="19" type="text" value="">
<br>
<span class="reqTxtInst">Apartment, suite, unit, building, floor, etc. </span>
<br><br>
Address Line 3:
<br>
<input class="modTxtMedium" name="address3" maxlength="50" size="19" type="text" value="">
<br>
<span class="reqTxtInst">Department, c/o, etc. </span>
<br><br>
<span class="pi-h4">City:</span>
<br>
<input class="modTxtMedium" name="city" maxlength="50" size="19" type="text" value="WALNUT">
<br><br>
<span class="pi-h4">State:</span>
<br>
<select name="state" class="modSelMedium" size="1">
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option SELECTED value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DC">District of Columbia</option>
<option value="DE">Delaware</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
<br><br>
<span class="pi-h4">Postal Code:</span>
<br>
<input class="modTxtMedium" name="postal" maxlength="9" size="19" type="text" value="">
<br><br>
</div>
</td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<div class="pi-m-pad">
<span class="pi-h4">Telephone:</span>
<br>
<span class="pi-h4"><input class="modTxtMedium" name="telephone" maxlength="15" size="19" type="text" value="">
<br>
</div>
</td>
<td>
Ext:
<br>
<input class="modTxtSmall" name="extension" maxlength="5" size="4" type="text" value="">
<br>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="pi-mA1-c-b-spc"><img alt="" border="0" height="1" src="/img/1.gif" width="1"></td>
</tr>
<tr>
<td class="pi-mA1-line"><img alt="" border="0" height="1" src="/img/1.gif" width="1"></td>
</tr>
<tr>
<td class="pi-mA1-b" valign="top">
<table border="0" cellpadding="0" cellspacing="0" width="422">
<tr>
<td class="pi-m-button" nowrap><input type="image" alt="Update" name="update_address" src="/img/en/btn_update.gif" WIDTH="51" HEIGHT="18"></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td width="10"><img alt="" border="0" height="1" src="/img/1.gif" width="10"></td>
<td valign="top" width="134"><img alt="" border="0" height="1" src="/img/1.gif" width="134"></td>
</tr>
</table>
</form>
&#13;
要填写表单,我只需执行此操作即可正常
IE.Navigate "https://wwwapps.ups.com/webClaims/address?addrtype=f&loc=en_US"
'IE.Document.Focus
Sleep (5000)
IE.Document.getElementsByName("companyName")(0).Value = "COMPANY"
IE.Document.getElementsByName("name")(0).Value = "n\a"
IE.Document.getElementsByName("address1")(0).Value = "222 address"
IE.Document.getElementsByName("postal")(0).Value = "99999"
IE.Document.getElementsByName("telephone")(0).Value = "111-111-1111"
但是当提交表单时,我遇到了麻烦。如您所见,提交表单会将您带到首先生成表单的URL
<form name="address" action="/webClaims/address" method="post">
所以难怪当我做
时表格不会出现在任何地方 IE.Document.getElementsByName("address")(0).submit
我正在查看他们拥有的javascript函数,并尝试了这个
Call IE.Document.parentWindow.execScript("submitRefresh", "JavaScript")
但结果相同。
当我尝试
时Call IE.Document.parentWindow.execScript("updateContact()", "JavaScript")
我收到此错误
Could not complete the operation due to error 80020101
我还试图点击显示提交表单的按钮
<input type="image" alt="Update" name="update_address" src="/img/en/btn_update.gif" WIDTH="51" HEIGHT="18">
但它表示input
标记无法点击。
我是否遗漏了HTML
或JavaScript
需要采用与通常不同的方法的内容?难道我对儿童窗户的理解不明白,这使我的方法变得不可行吗?
答案 0 :(得分:0)
事实证明,您可以像这样与子窗口进行交互,并通过其URL识别它
Dim objShell, IE_count, x, my_url, childPage
'Instead of opening the child window as if it were a web page with a url, treat it as a mere popup window
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next ' sometimes more web pages are counted than are open
'We find which window is the popup by looking at its url
my_url = objShell.Windows(x).Document.Location
'Debug.Print my_url
'The url of the popup
If my_url = "https://wwwapps.ups.com/webClaims/address?addrtype=f&loc=en_US" Then
'We found it, so make it a shell object for us to work with
Set childPage = objShell.Windows(x)
Exit For
End If
Next
我从here大量借用。
我可能试图通过获取其标题来获取子窗口,但它与父窗口具有相同的标题,因此url因此更好。之后,我可以填写表单并使用childPage
对象及其方法和属性提交。