使用以下代码,我可以打开Internet Explorer窗口并导航到该网站。我想更进一步,能够通过其中一个链接(如"Boxing/UFC")点击代码。
Sub Run()
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.Navigate2 "http://www.bet365.com"
Do Until .ReadyState = 4: DoEvents: Loop
.Quit
End With因为这个链接会导致一个Javascript onclick事件(onclick="gS(1020,9);return false;"),你能推荐一些我可以使用的代码,使VBA在运行VBA宏后可以在不受用户干扰的情况下点击这个链接吗?
发布于 2011-04-26 13:41:49
在Internet Explorer中,只需通过DOM单击链接即可。
检索您感兴趣的DOM节点( A),并对其调用click()。这还将触发所有关联的事件处理程序。
编辑:在你的例子中,使用VBA,像这样做(未测试,我这里没有VBA )
Dim sideNav As IHTMLElement
Dim navLinks As IHTMLElementCollection
Dim currLink As IHTMLElement
Set sideNav = IE.document.getElementByID("sln")
Set navLinks = sideNav.all.tags("A")
For Each currLink In navLinks
If Trim(currLink.innerText) = "Boxing/UFC" Then
currLink.Click
End If
Next currLink发布于 2011-10-10 23:46:08
您需要查询html文档,并检索集合。一旦检索到集合,就需要遍历元素并单击它。
我在这里粘贴了一段代码(click here for the entire code)。这是在c++中实现的,但我假设它对于VB也是一样的。
IHTMLElementCollection *spCollectEmbed;
spDocument->get_links(&spCollectEmbed);
if(spCollectEmbed)
{
// get all the links
long lLen;
spCollectEmbed->get_length(&lLen);
for (long i = 0; i < lLen; i++)
{
IDispatch *ppvdispOption;
IHTMLElement *interfaceHTMLElement;
VARIANT index;
index.vt = VT_I4;
index.lVal = i;
// get the item from the document
HRESULT hResult = spCollectEmbed->item(index, index, &ppvdispOption);
if(SUCCEEDED(hResult))
{
// query for the element
hResult = ppvdispOption->QueryInterface( IID_IHTMLElement,
(void **) &interfaceHTMLElement);
if(SUCCEEDED(hResult))
{
BSTR innerhtml;
interfaceHTMLElement->get_innerHTML(&innerhtml);
// click the links
interfaceHTMLElement->click();
Sleep(2000);
}https://stackoverflow.com/questions/5786271
复制相似问题