Search code examples
vbaexcelinternet-explorerie-automation

IE automation, automated login to a particular page


I am trying to automate login to a page through Internet Explorer with VBA code, however, I am stuck as I am not able to identify the login and password fields, as they do not have any ID or Tag name. Is there any way to use xPath or some other alternatives to solve this problem? Please see the login page screenshot: https://i.sstatic.net/UjJeS.png

xPath looks like this:

//*[@id="root"]/div/div[2]/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/input

Here is the html code of the page:

<div id="root">
         <div class="root__content">
            <div class="header1__wrap">
               <header class="header1"><a class="header1__logo" href="https://"></a></header>
            </div>
            <div class="content">
               <div class="content__main">
                  <div>
                     <div style="padding: 0px 10px;"></div>
                     <div class="grid">
                        <div class="gridone">
                           <div class="form" name="web/log_in">
                              <div class="form__title">Log In</div>
                              <div class="section">
                                 <div class="section__top"></div>
                                 <div class="section__content">
                                    <p class="link" name="sign_up_link"><a href="/sign_up">Register</a></p>
                                    <div class="edit selected" name="email">
                                       <div class="edit__icon">
                                          <i class="svg-icon">
                                             <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 612.011 612.011" width="50" height="44" version="1.1">
                                             </svg>
                                          </i>
                                       </div>
                                       <div class="edit__field">
                                          <div class="edit__label selected">Login/Email address</div>
                                          <input tabindex="0" type="text" value="" autoComplete="off">
                                       </div>
                                    </div>
                                    <div class="edit" name="password">
                                       <div class="edit__icon">
                                          <i class="svg-icon">
                                             <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 40 42" width="40px" height="42px" version="1.1">
                                             </svg>
                                          </i>
                                       </div>
                                       <div class="edit__field">
                                          <div class="edit__label">Password</div>
                                          <div style="display: none;"><input type="text"><input type="password"></div>
                                          <input tabindex="0" type="password" value="" autoComplete="off">
                                       </div>
                                       <div class="edit__pass">SHOW</div>
                                    </div>
                                    <p class="button link right" data-type="link" name="forgot"><span>Forgot password?</span></p>
                                 </div>
                                 <div class="section__bottom">
                                    <div class="button submit" data-type="submit" name="button_submit">
                                       <div>Login</div>
                                    </div>
                                 </div>
                              </div>
                           </div>
                        </div>
                     </div>
                  </div>
               </div>
            </div>

Solution

  • An HTMLElement ID is not a requirement for grabbing your elements. In your case, your username field has a class name edit__label selected and your password can be found by the element name password.

    See if this works for you:

    Dim doc As HTMLDocument
    Set doc = ieobj.document
    
    Dim oUser As Object, oPass As Object
    Set oUser = doc.getElementsByClassName("edit__label selected")(0)
    Set oPass = doc.getElementsByName("password")(0)
    
    '.Value may or may not work. Try both
    oUser.Value = "myUserName"
    oUser.innerText = "myUserName"
    
    '.Value may or may not work. Try both
    oPass.Value = "pa$$word"
    oPass.innerText = "pa$$word"