使用 VBA Excel 通过更改下拉值来导航网站
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16347898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using VBA Excel to navigate a website by changing dropdown value
提问by Michael
I am writing an Excel macro that will navigate to a utility company website and then download all of the statements linked to that account. To get to the statement download page, I need to navigate to each account's summary page. All of the accounts are listed in a dropdown - when I manually click a different account from the list, the page updates to the new account. Using the tip I got from this question, I am able to get the macro to change the dropdown value. The problem is, even though it chooses a new value from the list, the page doesn't update to the new account. Unfortunately, there is no direct link to each account's page as it looks like the dropdown passes a value to a script. I tried copying the URL down from different accounts, but no matter what account it's on the URL is the same. Here is VBA code in question:
我正在编写一个 Excel 宏,它将导航到公用事业公司网站,然后下载链接到该帐户的所有报表。要访问对帐单下载页面,我需要导航到每个帐户的摘要页面。所有帐户都列在下拉列表中 - 当我从列表中手动单击不同的帐户时,页面会更新为新帐户。使用我从这个问题中得到的提示,我能够获得宏来更改下拉值。问题是,即使它从列表中选择了一个新值,页面也不会更新到新帐户。不幸的是,没有指向每个帐户页面的直接链接,因为下拉菜单似乎将值传递给脚本。我尝试从不同的帐户向下复制 URL,但无论 URL 上的哪个帐户都是相同的。这是有问题的VBA代码:
Do While i < intNumberAcct
ie.document.getElementById("ctl00_PageContent_AccountDropDown_BillingAccountsDropDown").selectedindex = i
Call downloadStatement
i = i + 1
Loop
I tried refreshing IE after it changes the dropdown value, but when it refreshes it reverts back to the original value in the dropdown. I also tried ie.document.getElementById("ctl00_PageContent_AccountDropDown_BillingAccountsDropDown").Click
after it selected the new index, but it didn't have any effect.
我尝试在更改下拉值后刷新 IE,但是当它刷新时,它会恢复到下拉列表中的原始值。我ie.document.getElementById("ctl00_PageContent_AccountDropDown_BillingAccountsDropDown").Click
在它选择新索引后也尝试过,但没有任何效果。
Here is the html source code of the dropdown - I removed a large chunk of the accounts so it wouldn't be so long and obfuscated the account numbers and addresses with 'x':
这是下拉菜单的 html 源代码 - 我删除了一大块帐户,所以它不会那么长,并用“x”混淆了帐号和地址:
<table style="width: 100%" cellpadding="0" cellspacing="0">
<tr>
<td valign="top" class="AccountDropDownLabelCells" style="height: 25px;">
<strong>Billing Account:</strong>
</td>
<td valign="top" style="text-align: left" align="left">
<select name="ctl00$PageContent$AccountDropDown$BillingAccountsDropDown" onchange="javascript:setTimeout('__doPostBack(\'ctl00$PageContent$AccountDropDown$BillingAccountsDropDown\',\'\')', 0)" id="ctl00_PageContent_AccountDropDown_BillingAccountsDropDown" style="width:250px;">
<option value="xxxxx">xxxxx(16 xxxxx Dr)</option>
<option selected="xxxxx" value="xxxxx">xxxxx(18 xxxxxDr)</option>
<option value="xxxxx">xxxxx(20 xxxxx Dr)</option>
<option value="xxxxx">xxxxx(22 xxxxxDr)</option>
<option value="xxxxx">xxxxx(28 xxxxx Dr)</option>
<option value="xxxxx">xxxxx(30 xxxxxDr)</option>
<option value="xxxxx">xxxxx(34 xxxxxDr)</option>
</select>
</td>
</tr>
</table>
Any ideas on how to get the page to update to the new account after selecting it in the dropdown would be greatly appreciated.
关于如何在下拉列表中选择页面后更新到新帐户的任何想法将不胜感激。
采纳答案by Santosh
Once you set the index of dropdown you need to trigger the change event.
设置下拉索引后,您需要触发更改事件。
select name="ctl00$PageContent$AccountDropDown$BillingAccountsDropDown" onchange="javascript:setTimeout('__doPostBack(\'ctl00$PageContent$AccountDropDown$BillingAccountsDropDown\',\'\')', 0)"id="ctl00_PageContent_AccountDropDown_BillingAccountsDropDown" style="width:250px;"
选择名称="ctl00$PageContent$AccountDropDown$BillingAccountsDropDown" onchange="javascript:setTimeout('__doPostBack(\'ctl00$PageContent$AccountDropDown$BillingAccountsDropDown\',\'\')', 0)"id="ctl00_Down_PageContent_AccountDrop"AccountDrops style ="宽度:250px;"
doc.getElementById("xs_r_gender").selectedindex=1
doc.getElementById("xs_r_gender").FireEvent("onchange")