dcsimg
Results 1 to 5 of 5

Thread: getElementById Not Working

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2019
    Posts
    3

    getElementById Not Working

    Hello,

    Excuse the errors in my following statements if there are any, this is not my primary language to code in.

    (Background) To improve the speed of processing thousands of transactions (rather than manual entry one-by-one), I created a function within MS Excel which looped through each row (each row being an individual transaction). With each row, I navigated to a webpage, set several field values, submitted the form, and continued on to the next transaction. This process works FLAWLESSLY.

    Fast forward to today, and I need to extract the code from MS Excel and create a VB script which can run independently. However, when I try to run similar code from VBA, I cannot seem to get the desired field to fill. From the simple script below, I only want to set the text field of 'Control_Key', but it's as if the script doesn't even recognize the field being on the form.

    I have also added a snippet of the HTML for the webpage, however I need to stay quite vague due to copyright reasons.


    Code:
    Sub FormFill()
    
        Dim IE
        Set IE = CreateObject("InternetExplorer.Application")
        IE.Visible = True
        IE.navigate "InsertUrlHere"
    
        WScript.Sleep 5000
    
        IE.document.getElementById("Control_Key").value = "ValueToEnter"
    
    End Sub
    Code:
    <html>
        <body>
            <form name="MainForm">
                <table class="HeaderBar">
                    <tbody>
                        <tr>
                            <td>
                                <input name="Control_Key">
                            </td>
                        </tr>
                    </tbody>
                </table>
            </form>
        </body>
    </html>
    I'm hoping I'm overlooking something very simple and that this will be an easy fix.

    Thanks!
    Last edited by datamagician; Aug 13th, 2019 at 06:48 PM.

  2. #2
    Fanatic Member
    Join Date
    Feb 2003
    Posts
    721

    Re: getElementById Not Working

    Quote Originally Posted by datamagician View Post
    Hello,

    Excuse the errors in my following statements if there are any, this is not my primary language to code in.

    (Background) To improve the speed of processing thousands of transactions (rather than manual entry one-by-one), I created a function within MS Excel which looped through each row (each row being an individual transaction). With each row, I navigated to a webpage, set several field values, submitted the form, and continued on to the next transaction. This process works FLAWLESSLY.

    Fast forward to today, and I need to extract the code from MS Excel and create a VB script which can run independently. However, when I try to run similar code from VBA, I cannot seem to get the desired field to fill. From the simple script below, I only want to set the text field of 'Control_Key', but it's as if the script doesn't even recognize the field being on the form.

    I have also added a snippet of the HTML for the webpage, however I need to stay quite vague due to copyright reasons.


    Sub FormFill()

    Dim IE
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.navigate "InsertUrlHere"

    WScript.Sleep 5000

    IE.document.getElementById("Control_Key").value = "ValueToEnter"

    End Sub

    <html>
    <body>
    <form name="MainForm">
    <table class="HeaderBar">
    <tbody>
    <tr>
    <td>
    <input name="Control_Key">
    </td>
    </tr>
    </tbody>
    </table>
    </form>
    </body>
    </html>

    I'm hoping I'm overlooking something very simple and that this will be an easy fix.

    Thanks!
    A few tips:
    1. Use code tags when posting code. (The "#" button when you're writing a post.)
    2. Use a HTML validator, that code doesn't look complete.
    3. Dispose of objects created in your script by setting them to "Nothing" when your code no longer needs them. "Set IE = Nothing" for example.
    4. Is that all your VBScript code? If so, you need to put "Call FormFill" at the start.
    5. Are you getting any error messages? I am getting:
    Code:
    ---------------------------
    Windows Script Host
    ---------------------------
    Script:	D:\t.vbs
    Line:	12
    Char:	5
    Error:	Object required: 'IE.document.getElementById(...)'
    Code:	800A01A8
    Source: 	Microsoft VBScript runtime error
    
    ---------------------------
    OK   
    ---------------------------
    when trying your code. (With a call to "FormFill" added.)

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2019
    Posts
    3

    Re: getElementById Not Working

    Quote Originally Posted by Peter Swinkels View Post
    A few tips:
    1. Use code tags when posting code. (The "#" button when you're writing a post.)
    2. Use a HTML validator, that code doesn't look complete.
    3. Dispose of objects created in your script by setting them to "Nothing" when your code no longer needs them. "Set IE = Nothing" for example.
    4. Is that all your VBScript code? If so, you need to put "Call FormFill" at the start.
    5. Are you getting any error messages? I am getting:
    Code:
    ---------------------------
    Windows Script Host
    ---------------------------
    Script:	D:\t.vbs
    Line:	12
    Char:	5
    Error:	Object required: 'IE.document.getElementById(...)'
    Code:	800A01A8
    Source: 	Microsoft VBScript runtime error
    
    ---------------------------
    OK   
    ---------------------------
    when trying your code. (With a call to "FormFill" added.)
    Thanks for the tips Peter.

    1. My post is modified to read easier now.

    2. You're absolutely correct, the HTML should be better with this:

    Code:
    <HTML>
    <BODY> 
    <TABLE class="Bar1">
      <TBODY>
      <TR>
        <TD nowrap="" 
    valign="middle"><INPUT name="SubmitButton" class="BUTTON" type="submit" value="Submit"> 
              </TD></TR></TBODY></TABLE>
    <TABLE class="Bar2">
      <TBODY>
      <TR>
        <TD nowrap="" colspan="2"><INPUT name="Control_Key" onfocus="selectAll(this);"> 
        </TD></TR></TBODY></TABLE></BODY></HTML>
    3. I only posted part of my code. I do dispose of it at the end of the script.

    4. I only posted part of my code. FormFill is getting called at the beginning of the script.

    5. No errors are displayed. IE opens, but nothing happens after that. I'm expecting the field to be filled.


    I ran my piece of code against the new HTML (posted above) and still have the same issue.

    Also, as stated previously, I had this process working perfectly within MS Excel using the following function (along with a large amount of other code):

    Code:
    IE.document.getElementById("Control_Key").Value = Cells(2, 1).Value

  4. #4
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,061

    Re: getElementById Not Working

    The input element has a name attribute set to Control_Key but you haven't set the id try setting the id attribute.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2019
    Posts
    3

    Re: getElementById Not Working

    Quote Originally Posted by PlausiblyDamp View Post
    The input element has a name attribute set to Control_Key but you haven't set the id try setting the id attribute.
    Unfortunately, I have tried that as well. No such luck.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width