vba 如何使用VBA点击网站上的图片链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22716894/
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
How to use VBA to click a image link on a website?
提问by Crayyyg
I'm trying to pull documents from an internal website for multiple accounts at a time.
我正在尝试一次从多个帐户的内部网站中提取文档。
I'm using VBA in Excel 2010. I created the code to login and navigate until I get to the part where I need to click an image button.
我在 Excel 2010 中使用 VBA。我创建了用于登录和导航的代码,直到到达需要单击图像按钮的部分。
Below is the related source code. First there is a function:
下面是相关的源代码。首先有一个函数:
<script language="javascript" type="text/javascript">
<!--
function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
theform = document.Form1;
}
else {
theform = document.forms["Form1"];
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
// -->
</script>
And then skipping down there is this block of code where "Print.gif" is the button to click:
然后跳过下面这段代码,其中“Print.gif”是要单击的按钮:
<LINK rel="stylesheet" type="text/css"
href="/crystalreportviewers10/css/default.css">
<table id="CrystalReportViewer1" cellpadding="0" cellspacing="0"
style="height:1043px;width:901px;Z-INDEX: 101;LEFT: 8px; POSITION: absolute; TOP: 8px">
<tr><td>
<div style="width: 901px; height: 1043px;position:relative">
<div style="height:30px;width:901px;top:0px;left:0px;">
<table class="crtoolbar" cellspacing=0 cellpadding=0><tr nowrap>
<td nowrap width=16> </td>
<td nowrap width=24px>
<input type="image" name="CrystalReportViewer1:_ctl2:_ctl0"
title="Show/Hide Group Tree"
onmouseover="this.src='/crystalreportviewers10/images/toolbar/grouptree_over.gif'"
onmouseout="this.src='/crystalreportviewers10/images/toolbar/grouptree.gif'"
src="/crystalreportviewers10/images/toolbar/grouptree.gif" alt="" border="0"
style="height:24px;width:24px;" /></td>
<td nowrap width=16> </td>
<td nowrap width=24px>
<img title="Print"
onclick="javascript:__doPostBack('CrystalReportViewer1:_ctl2:_ctl3','')"
onmouseover="this.src='/crystalreportviewers10/images/toolbar/print_over.gif'"
onmouseout="this.src='/crystalreportviewers10/images/toolbar/print.gif'"
src="/crystalreportviewers10/images/toolbar/print.gif" alt="" border="0"
style="height:24px;width:24px;" /></td>
I've tried a few different ways to fire the "onclick" but I'm having trouble because it looks like it passes to the above function to navigate to the correct page. So far I've tried:
我尝试了几种不同的方法来触发“onclick”,但我遇到了麻烦,因为它看起来像传递给上述函数以导航到正确的页面。到目前为止,我已经尝试过:
For Each obj In IE.Document.all
' MsgBox obj.innerHTML
If InStr(obj.innerHTML, "Print") > 0 Then
obj.Click
' Exit For
End If
Next
or
或者
For Each obj In IE.Document.all
If obj.Name = "Print" Then
obj.Click
Exit For
End If
Next
and a couple variations that have failed me so far. Any help on this would be great. Thanks.
以及到目前为止让我失望的几个变体。对此的任何帮助都会很棒。谢谢。
回答by Santosh
Try below code and let us know the results.
试试下面的代码,让我们知道结果。
For Each obj In ie.document.all
If InStr(obj.innerHTML, "Print") > 0 Then
obj.document.parentWindow.execScript "javascript:__doPostBack('CrystalReportViewer1:_ctl2:_ctl3','')"
End If
Next
OR
或者
For Each obj In ie.document.all
If InStr(obj.innerHTML, "Print") > 0 Then
obj.Click
obj.FireEvent ("onclick")
End If
Next