VBA 与网页上的单选按钮交互
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15579830/
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
VBA Interacting With Radio Buttons On Webpage
提问by ARich
EDIT: A few months later I attempted this again using the same syntax I had tried the first time (posted below). For some reason, it worked! Maybe something other than my syntax was causing the ineffectiveness... END EDIT
编辑:几个月后,我再次尝试使用我第一次尝试过的相同语法(发布在下面)。出于某种原因,它奏效了!也许我的语法以外的其他东西导致了无效......结束编辑
I've been searching forums for a couple of hours now trying to find a solution to this problem, but none of the things I've tried work.
我已经在论坛上搜索了几个小时,试图找到解决此问题的方法,但我尝试过的所有方法都没有奏效。
I'm using VBA to automate the process of creating a survey on SurveyMonkey. So far I've been able to:
我正在使用 VBA 来自动化在 SurveyMonkey 上创建调查的过程。到目前为止,我已经能够:
- Log into my account,
- Click several hyperlinks to create a new response collector,
- Name the collector,
- Go to the collector settings,
- And, select three of four radio buttons to change collector settings.
- 登录我的账户,
- 单击多个超链接以创建新的响应收集器,
- 命名收集器,
- 转到收集器设置,
- 并且,选择四个单选按钮中的三个来更改收集器设置。
The issue isn't that I can't select the radio buttons; my code selects the first three buttons just fine. What puzzles me is that the fourth button doesn't change! I use the same process for each button, so I can't figure out why the last button won't select.
问题不在于我无法选择单选按钮;我的代码选择前三个按钮就好了。令我困惑的是,第四个按钮没有改变!我对每个按钮使用相同的过程,所以我无法弄清楚为什么最后一个按钮不会被选中。
Here's the section of my code:
这是我的代码部分:
objIE.Document.getElementById("rdlResponseType_1").Click 'Allow multiple responses = Yes
objIE.Document.getElementById("rdlResponseEdit_1").Click 'Allow Responses to be Edited = Yes
objIE.Document.getElementById("rdlThankyou_1").Click 'Display a "Thank You" page? = Yes
objIE.Document.getElementById("rdlCompleteOpt_1").Click 'Survey Completion = Close Window
This is the HTML for the radio buttons:
这是单选按钮的 HTML:
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%;">
<tr id="CompleteOptDesc">
<td style=""> </td>
<td>After the respondent leaves the survey:</td>
</tr>
<tr id="CompleteOptItems">
<td style=""> </td>
<td align="left" nowrap="nowrap" valign="top">
<table id="rdlCompleteOpt" class="Clean radioList" OnClick="radioToggle('rdlCompleteOpt', '0', 'panLink');" name="rdlCompleteOpt" border="0" style="white-space: nowrap">
<tr>
<td><span style="white-space: nowrap"><input id="rdlCompleteOpt_0" type="radio" name="rdlCompleteOpt" value="0" checked="checked" /><label for="rdlCompleteOpt_0"><b>Redirect</b> to your own webpage.</label></span></td>
</tr><tr>
<td><input id="rdlCompleteOpt_1" type="radio" name="rdlCompleteOpt" value="2" /><label for="rdlCompleteOpt_1"><b>Close Window</b></label></td>
</tr>
</table>
Here is the code for the toggle section of the radio:
这是收音机切换部分的代码:
<td width="65%" valign="top">
<div id="panLink" style="DISPLAY:inline">
<div class="URLinfo">
<b>Enter a URL</b> to jump to upon leaving the survey:<br />
<div title="REQUIRED: Enter URL (255 character max)" style="padding:4px 0 2px 0;">
<input name="txtWebLink" type="text" value="http://www.surveymonkey.com/" maxlength="255" size="40" id="txtWebLink" class="RQR opaque" />
</div>
<span class="tip">Example: <u>http://www.mysite.com/home.html</u></span>
</div>
</div>
</td>
Any suggestions would be very appreciated!
任何建议将不胜感激!
回答by Shawn E
There's a couple things to consider:
有几件事情需要考虑:
When are you calling this code? OnNavigate or DocumentComplete? It may be that the object doesn't exist yet. You can check to see if it does by using a breakpoint and adding a watch for the expressions
objIE.Document.getElementById("rdlCompleteOpt_1")
Use OnDocumentComplete to ensure the HTML components exist
It's possible the way you are calling the click function is causing an issue. Try this
Call objIE.Document.getElementById("rdlCompleteOpt_1").Click()
I doubt that it is likely to have much impact but I like to verify everything.
Isolate the cause of the issue by removing the 'layers'. Can you accomplish this using pure JavaScript that's invoked from the IE dev tools (F12) JavaScript console?
If no then the issue is with how you're interacting with the page elements. If yes then we need to figure out what it is with the VBA intaction is causing an issue. Again see #2 as it is likely the cause of the issue.
Also, does the issue occur if you change the HTML order of the elements? What if you call it this way?
objIE.Document.getElementById("rdlCompleteOpt_1").Click 'Survey Completion = Close Window objIE.Document.getElementById("rdlResponseType_1").Click 'Allow multiple responses = Yes objIE.Document.getElementById("rdlResponseEdit_1").Click 'Allow Responses to be Edited = Yes objIE.Document.getElementById("rdlThankyou_1").Click 'Display a "Thank You" page? = Yes
Does it work then or does the "last" item fail?
Highly unlikely to be the cause but the HTML supplies is missing the code to close cell, row and table.
</tr> </table> <!-- missing --> </td> </tr> </table>
While not the specific answer to your question I believe this may be inline with the intent of your post. Since you want to manage a surveymonkey collector take a look at the SurveyMonkey API hereas using an API gives you quite a few benefits.
- you don't have to rely on user elements which may change or be changed outside of your control
- you can use a controlled and "contracted" interface to manage the information which covers you for any future changes (assuming they don't deprecate and stop supporting the API)
你什么时候调用这个代码?OnNavigate 还是 DocumentComplete?可能是该对象还不存在。您可以通过使用断点并为表达式添加监视来检查它是否存在
objIE.Document.getElementById("rdlCompleteOpt_1")
使用 OnDocumentComplete 确保 HTML 组件存在
您调用 click 函数的方式可能会导致问题。尝试这个
Call objIE.Document.getElementById("rdlCompleteOpt_1").Click()
我怀疑它可能会产生多大影响,但我喜欢验证一切。
通过删除“层”来隔离问题的原因。您能否使用从 IE 开发工具 (F12) JavaScript 控制台调用的纯 JavaScript 来完成此操作?
如果不是,那么问题在于您如何与页面元素进行交互。如果是,那么我们需要弄清楚 VBA 完整性是什么导致了问题。再次参见 #2,因为这可能是问题的原因。
另外,如果您更改元素的 HTML 顺序会出现问题吗?如果你这样称呼它呢?
objIE.Document.getElementById("rdlCompleteOpt_1").Click 'Survey Completion = Close Window objIE.Document.getElementById("rdlResponseType_1").Click 'Allow multiple responses = Yes objIE.Document.getElementById("rdlResponseEdit_1").Click 'Allow Responses to be Edited = Yes objIE.Document.getElementById("rdlThankyou_1").Click 'Display a "Thank You" page? = Yes
那么它是否有效还是“最后一个”项目失败了?
极不可能是原因,但 HTML 供应缺少关闭单元格、行和表格的代码。
</tr> </table> <!-- missing --> </td> </tr> </table>
虽然不是您问题的具体答案,但我相信这可能符合您帖子的意图。由于您想管理surveymonkey 收集器,因此请查看此处的SurveyMonkey API,因为使用API 可为您带来很多好处。
- 您不必依赖可能会更改或更改超出您控制范围的用户元素
- 您可以使用受控和“签约”界面来管理涵盖您未来任何更改的信息(假设他们不弃用并停止支持 API)