Question

I am trying to click a link on a page, it don't have any id and not having a unique class name. Only unique thing about the function is the onclick handler

<a href="#" onclick="closepopup('popup', 'popuphandler')" > </a>

I need to click this link via vbscript automation, for the same i tried the code as:

Set allLinks = ie.document.links
for j = 0 to allLinks.length-1
    if allLinks(j).onClick = "closePopup(""popup"",""popupClose"")" then
        allLinks(j).click
        j = allLinks.length 'class name = blueButtonCenter
    end if
Next

But it is not working, please help.

Thanks in advance :)

Was it helpful?

Solution

I would try to check if the closePupup is found in the actual javascript, like so:

'safety first
if not isnull(allLinks(j).onClick) then
  if instr(allLinks(j).onClick.toString() ,  "closePopup(""popup"",""popupClose"")" ) > -1 then 
        allLinks(j).click 
        j = allLinks.length 'class name = blueButtonCenter 
  end if 
end if

This assumens onClick delivers a string, (at least in javascript it does and in IE9 and Chrome this works)

OTHER TIPS

You didn't specify what doens't work, finding the link or clicking it, in the first case: onClick wil return something like

about:blank# function onclick()
{
closepopup('popup', 'popuphandler')
}

so you shoudl use instr to check if it is the right link

Dim HTMLDoc, XML, URL, table
Set HTMLDoc = CreateObject("HTMLFile")
Set XML = CreateObject("MSXML2.XMLHTTP")

URL = "your url"

With XML
  .Open "GET", URL, False
  .Send
  HTMLDoc.Write .responseText
End With

Set allLinks = HTMLDoc.links 
For each link in allLinks
  if instr(link.onClick, "closepopup('popup', 'popuphandler')") then
    link.click
    exit for
  end if
Next 

Only the click won't work this way..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top