Using sendkeys and loop to download files

  • Hi All!


    First time poster, long time troll of this forum for solutions! :P


    I'm still a novice, and come mostly figure out the solutions to my problems with some searching, but controlling a web page is something very new to me and I need some help!


    I have created some code that directs me to a web page, and i use send keys to navigate my way to the input field. In this input field I am trying to put a cell value in a range use send keys to download the file, and the repeat the process for the next cell.


    Here is my code so far, it does everything right up until pasting the copied cell into the input section of the web page - the issue is it is pasting nothing :(



    Would love some help if possible!


    Thanks :)


    Steph

  • Re: Using sendkeys and loop to download files


    Hi and welcome to the forum.


    I don't really want to discourage you but I would strongly urge you not to use SendKeys - it is not a reliable method and will likely get you into trouble at some stage. You would be better advised, IMO, to use an Object variable to refer to InternetExplorer and proceed on that basis. You should then find it easier to navigate the webpage in question. Do you have a particular webpage (so that we can see what you are trying to achieve) or is it a confidential/works page?

  • Re: Using sendkeys and loop to download files


    Hi Richie,


    Thanks for your reply!


    I had a feeling using sendkeys probably wasnt the best method - there was an incident where the loop ran off the web page and started randomly doing things on my computer. I think this was because i didn't leave ample time for the page to load.


    As far as I am aware, you can only use Object variable references with InternetExplorer, and unfortunately the web page I am working isnt compatible with I.E so chrome or mozilla has to be used.


    Also, it is a confidential work page so i cant disclose much to assist you further However im more than happy to put more detailed comments in the code - maybe even make a dummy page in excel of what the web page looks like if need be!


    Thanks


    S

  • Re: Using sendkeys and loop to download files


    Update:


    I was able to get the loop to work, however I need to manually activate the excel window for each loop otherwise it just stays on the firefox page.


    Here is the updated code:




    any suggestions how to toggle the windows automatically?

  • Re: Using sendkeys and loop to download files


    Hi,


    It looks as if automation using FireFox is not really an option. There was an attempt to make it usable in the same way as IE by use of a plugin, see here : http://www.adamlock.com/mozilla/ . However, it looks as if that isn't being maintained currently so it may not support the current version of FF.


    Are you sure your webpage won't work with IE? It really would be much easier and safer than using SendKeys (which is basically an accident waiting to happen!).

  • Re: Using sendkeys and loop to download files


    Unfortunately the website is not compatible at all with IE, its a shame - this would have saved me so much time!

  • Re: Using sendkeys and loop to download files


    Good News - I way playing around with IE and when I switch it to compatibility view - the website seems to work! Do you know any predefined code that would allow me to loop through a range of values and download the files? :)

  • Re: Using sendkeys and loop to download files


    Hi,


    That is good news - should make things much easier for you.


    The following should help to get you started. I've used http://www.fool.co.uk/index.aspx as an example site. If you register there (it's free and just for the purposes of illustration). The following code will login, using the details provided, and navigate to a given page.



    It's a bit of a mish-mash of exisiting routines that I have just to serve as an illustration. The initial routine came from here : http://www.dailydoseofexcel.co…te-that-requires-a-login/ . Note that you will need to set a couple of References in the VBE.

  • Re: Using sendkeys and loop to download files


    Great! Thanks for pointing me in the right direction!


    Theres just one issue I'm facing that's preventing me from experimenting with this - it keeps on producing an error at ".submit" - runtime error 438 to be specific!



    This is preventing me from actually getting in to the website at all :( I've checked the source code of the website and it does say "type=submit" so I'm not sure where I'm going wrong!

  • Re: Using sendkeys and loop to download files


    Hi,


    Would you:
    1. Post the routine that you are currently using (replace your company details with alternatives for illustration but keep the code the same).
    2. Confirm that you have set the required references.

  • Re: Using sendkeys and loop to download files


    Sorry for the delayed reponse! I've been caught up in work :(


    Here is my code (sensitive data removed):


    And this is the source code:
    [CODE]



    <form id="loginForm" action="/Action/login/" method="post"> <label for="email">Email:</label> <div class="fields"> <input name="email" type="text" /> </div> <label for="jPassword">Password:</label> <div class="fields"> <input id="jPassword" name="password" type="password" /> <input id="submit" name="login" value="Login" type="submit" /> <a id="forgotpasswordlink" href="/action/passwordreset/>I forgot my password</a> </div>


    Cheeers!

  • Re: Using sendkeys and loop to download files


    Quote from Richie(UK);617635

    Also, did you try the approach I posted above (post #9) using HTMLInputElements?


    And (desperately clutching at straws!) possibly try replacing
    .submit


    with
    ieDoc.All("Login").Click


    Awesome!


    That one seemed to work :)


    Now just one more roadblock I'm facing before I can get out of your hair!


    I trying to to download a file however the are multiple fields with the same Download button the webpage and i dont know how to differentiate the code to select the right download button:


    Here is the code:


    Code
    ieDoc.getelementbyid("reportViews[4].salesforceId").Value = ActiveWorkbook.Sheets(1).Range("a1").Value
                ieDoc.getelementbyid("Download").Click


    and this is the source code:


    <div style="width:100px;float:left;">
    <label for="reportViews[3].merchantId">Merchant Id</label>
    </div>
    <div>
    <input name="reportViews[3].merchantId" type="text" />
    </div>
    </td>
    <td><input style="cursor: pointer;" name="download" value="Download" type="submit" /></td>
    <div style="display: none;"><input type="hidden" name="_sourcePage" value="K17HC9c_LAtihQVaG-0SqtOquypjt4NxHUl8zoS6wrfoZzO8z73vF5DIKlxhD-rO" /><input type="hidden" name="__fp" value="hQ9lByjJf8E=" /></div></form>
    </tr>

    <tr>
    <form action="/action/reports" method="post">
    <input name="reportType" value="finishedDealCodesbySalesforce" type="hidden" />
    <td>Finished Deal Codes</td>
    <td>



    <div style="width:100px;float:left;">
    <label for="reportViews[4].salesforceId">Salesforce CDA Number</label>
    </div>
    <div>
    <input name="reportViews[4].salesforceId" type="text" />
    </div>
    </td>
    <td><input style="cursor: pointer;" name="download" value="Download" type="submit" /></td>
    <div style="display: none;"><input type="hidden" name="_sourcePage" value="no--DrgyX79ihQVaG-0SqtOquypjt4NxHUl8zoS6wrfoZzO8z73vF5DIKlxhD-rO" /><input type="hidden" name="__fp" value="hlqtTRmAt9I=" /></div></form>
    </tr>

    </tbody>
    </table>
    </div>
    </div>
    </div>
    </div>
    <div>


    Thanks for all your help and patience!


    S

  • Re: Using sendkeys and loop to download files


    Hi Richie :)


    The code does work - however there are 5 identical download buttonds and I need the 4th download button selected. The code only selects the first instance unfortunately.

  • Re: Using sendkeys and loop to download files


    Hi again,


    It's weird coming up with code and not being able to test it! Do me a favour? When we are finished post the final routine (less any confidential stuff of course) just for reference in case anybody else has similar issues and uses the Search)


    Anyway, how about adding:


    Code
    Set inputCollection = ieDoc.getElementsByTagName("download")
     
        For Each inputElement In inputCollection
            If inputElement.getAttribute("value") = "no--DrgyX79ihQVaG-0SqtOquypjt4NxHUl8zoS6wrfoZzO8z73vF5DIKlxhD-rO" Then
                inputElement.Click
                Exit For
            End If
        Next inputElement
  • Re: Using sendkeys and loop to download files


    Im sorry - i know if must be a little frustrating!


    this is my code so far:



    It is producing a runtime error 438 on the following line:


    Code
    For Each inputElement In inputCollection


    any suggestions?

  • Re: Using sendkeys and loop to download files


    Hi,


    I've deleted the data that looked like it was personal/private (you may wish to consider changing your password, just in case).


    As for the code, how about changing the Set line to :

    Code
    Set inputCollection = ieDoc.forms(0).getElementByID("download")


    I think I picked up the wrong value before, so try:

    Code
    Set inputCollection = ieDoc.forms(0).getElementByID("download")
         
        For Each inputElement In inputCollection
            If inputElement.getAttribute("value") = "Download" Then
                inputElement.Click
                Exit For
            End If
        Next inputElement

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!