Click on a button/link (Javascript) on a website

  • Hello!


    I am trying to download a report from a website using VBA and Internet Explorer as the web browser.


    I have found some other threads that was interesting, but I was unable to make use of their examples:


    http://www.ozgrid.com/forum/showthread.php?t=158092


    http://stackoverflow.com/quest…n-an-html-javascript-page


    The first step for me is to click a link called "Download"


    Next to the "Download" link there is also a link to refresh "Refresh" and a "Printer Friendly" link, and this is the HTML code for this part of the page:



    I have used Firefox "Inspect element" and this is a part of the HTML code that is highlighted when I right click on the "Download" link:



    HTML
    1. <a name="ReportLinkMenu" href="javascript:void(null)" onclick="return NQWPopupMenu(event,'idDownloadLinksMenuo:go~r:report')">
    2. Download
    3. </a>


    If I left click on "Download" this HTML code is highlighted (and a small box appears with links to the various save file alternatives):


    Highlighted code:


    HTML
    1. <div style="position: absolute; top: 2369px; left: 170px; visibility: visible;" binit="true" id="idDownloadLinksMenuo:go~r:report" class="NQWMenu" onmouseover="NQWMenuMouseOver(event)">
    2. <table class="menuShadowWrapper" cellspacing="0px">
    3. <a style="width: 147px;" class="NQWMenuItem NQWMenuItemHighlight" name="SectionElements" href="javascript:void(null);" onclick="NQWClearActiveMenu();Download('saw.dll?Go&amp;ViewID=o\x253ago\x257er\x253areport&amp;Action=Download&amp;SearchID=umnpgp02b3lllfpgbpejqpq13q&amp;Style=ondemand&amp;Options=rfd&amp;ViewState=uek9kfkadiarc36omhhaod3jiq&amp;ItemName=Sales_Funnel_all\x2520peroiod\x2520and\x2520truck\x2527s\x2520groups_C&amp;Format=mht&amp;Extension=.xls'); return false">
    4. Download to Excel
    5. </a>


    Below is the HTML code connected to all save file alternatives, which the code above is a subset of:



    I would like to save the file as an .xls document, therefore I want to click the "Download to Excel". If I inspect the "Download to Excel", this HTML code is highlighted:


    HTML
    1. <a style="width: 147px;" class="NQWMenuItem" name="SectionElements" href="javascript:void(null);" onclick="NQWClearActiveMenu();Download('saw.dll?Go&amp;ViewID=o\x253ago\x257er\x253areport&amp;Action=Download&amp;SearchID=umnpgp02b3lllfpgbpejqpq13q&amp;Style=ondemand&amp;Options=rfd&amp;ViewState=uek9kfkadiarc36omhhaod3jiq&amp;ItemName=Sales_Funnel_all\x2520peroiod\x2520and\x2520truck\x2527s\x2520groups_C&amp;Format=mht&amp;Extension=.xls'); return false">
    2. Download to Excel
    3. </a>


    If I then left click on the "Download to Excel" in Internet Explorer, I get the usual "open or save as" pop up window that allows me to save the file.


    Would it be possible to locate and activate/click these objects by VBA (1. "Download" 2. "Download to Excel" and then 3. Save as .xls in a local folder)?

  • Re: Click on a button/link (Javascript) on a website


    Quote from Alexander12;719752

    Would it be possible to locate and activate/click these objects by VBA (1. "Download" 2. "Download to Excel" and then 3. Save as .xls in a local folder)?

    Yes, this is possible. The VBA code is available in the workbook in this post - http://www.ozgrid.com/forum/sh…78884&p=666927#post666927. Note though that the code which automates IE and causes the IE File Download window to appear is specific to the particular site in that thread. The code required to automate your web site will be different, though you can use some of the code for ideas and to get started. Without the URL it's difficult to help.


    Also, if the site is .asp or .aspx you may be able to download the .xls file invisibly using XMLhttp GET and/or POST requests, instead of automating IE. This technique may also work with .php sites, though I've never tried it.

  • Re: Click on a button/link (Javascript) on a website


    Thank you for your support.


    I have managed to write the first part of the VBA code that allowed me to login to the web site and access the report page.
    I used Firefox "Inspect element" to determine the id of each field (login, password and submit).


    Code
    1. IE.Document.getElementById("something1").Value = "something" 'Login
    2. IE.Document.getElementById("something2").Value = "something" 'Password
    3. IE.Document.getElementById("something3").Click 'Submit button


    But it seems to be a lot trickier to determine the id/name or unique identifier for the "Download" links.


    I had difficulties figuring out how to use your code in my case.


    So, do you think that I will need to use a similar method as the one you recommended our do you think that the "getElementById" could be used in this case based on the HTML code?



    HTML code for "Download"


    HTML
    1. <a name="ReportLinkMenu" href="javascript:void(null)" onclick="return NQWPopupMenu(event,'idDownloadLinksMenuo:go~r:report')">
    2. Download
    3. </a>


    HTML code for "Download to Excel"


    HTML
    1. [<a style="width: 147px;" class="NQWMenuItem" name="SectionElements" href="javascript:void(null);" onclick="NQWClearActiveMenu();Download('saw.dll?Go&amp;ViewID=o\x253ago\x257er\x253areport&amp;Action=Download&amp;SearchID=umnpgp02b3lllfpgbpejqpq13q&amp;Style=ondemand&amp;Options=rfd&amp;ViewState=uek9kfkadiarc36omhhaod3jiq&amp;ItemName=Sales_Funnel_all\x2520peroiod\x2520and\x2520truck\x2527s\x2520groups_C&amp;Format=mht&amp;Extension=.xls'); return false">
    2. Download to Excel
    3. </a>



    Anyway, thank you very much for the support you have already given.

  • Re: Click on a button/link (Javascript) on a website


    could you not use the XMLHTTP object?


  • Re: Click on a button/link (Javascript) on a website


    You could try getElementsByName as shown by the following code. Note the 's' plural, so it returns an array/collection of elements matching the specified name. To access or refer to a specific array element use the VBA arrayName(n) syntax.



    The code may need to wait until the "Download to Excel" link exists before clicking it, in which case replace the 2nd pair of Set downloadLink and downloadLink.Click lines with:

    Code
    1. Dim sectionElements As IHTMLElementCollection
    2. Do
    3. Set sectionElements = HTMLdoc.getElementsByName("SectionElements")
    4. DoEvents
    5. Loop While sectionElements Is Nothing
    6. Set downloadLink = sectionElements(0)
    7. downloadLink.Click


    The code requires references to MS Internet Controls and MS HTML Object Library, which gives you intellisense editing in the VBA editor, making code development far easier. Set these 2 references in the menu Tools - References in the VBA editor.

  • Re: Click on a button/link (Javascript) on a website


    Thank you John_w for your suggestions.


    I am now able to click both the "Download" and "Download to Excel" buttons thanks to you, so once again, thank you.


    Everything works the way it should now, the only thing I need to solve now, is the control of the Internet Explorer "Open or Save" dialog box that appears in the bottom of the screen after the last "downloadLink.Click" is executed:


    "Do you want to open or save something.xls from something.com?"


    This dialog box has three buttons: Open, Save and Cancel, where the save button is a drop down box with; Save, Save as, and save and open.


    Do you have any ideas on how to control this dialog box?


    Working code so far:


  • Re: Click on a button/link (Javascript) on a website


    Quote from Alexander12;720128

    Everything works the way it should now, the only thing I need to solve now, is the control of the Internet Explorer "Open or Save" dialog box that appears in the bottom of the screen after the last "downloadLink.Click" is executed:


    "Do you want to open or save something.xls from something.com?"


    This dialog box has three buttons: Open, Save and Cancel, where the save button is a drop down box with; Save, Save as, and save and open.

    It looks like you have IE9 or higher. My Windows API code which automates the various download windows only works with IE8 and I have never tried it with IE9 or higher. It is possible that the dialog box at the bottom of the screen doesn't appear if you add the URL to IE's trusted sites, though again I've never tried this. Otherwise you would need to identify the window captions and class names using a utility such as Winspector Spy, and modify the Win API code accordingly.

  • Re: Click on a button/link (Javascript) on a website


    Quote from John_w;720228

    It looks like you have IE9 or higher. My Windows API code which automates the various download windows only works with IE8 and I have never tried it with IE9 or higher. It is possible that the dialog box at the bottom of the screen doesn't appear if you add the URL to IE's trusted sites, though again I've never tried this. Otherwise you would need to identify the window captions and class names using a utility such as Winspector Spy, and modify the Win API code accordingly.


    Thank you for your suggestions John_w, I will look into that. In the meanwhile I have used SendKeys to solve this issue for me.


    I have read that this not the best option, but it seems to be working at the moment at least.


    I would not have been able to solve this without your help John_w, so thank you so much.


    Please see below for the last part of the VBA code:


    Code
    1. Application.Wait Now + TimeValue("00:00:01")
    2. SendKeys "{TAB}" Application.Wait Now + TimeValue("00:00:01")
    3. SendKeys "{TAB}" Application.Wait Now + TimeValue("00:00:01")
    4. SendKeys "{TAB}" Application.Wait Now + TimeValue("00:00:01")
    5. SendKeys "{ENTER}"