Home » excel » excel – Fill two input fields in a webpage

excel – Fill two input fields in a webpage

Posted by: admin May 14, 2020 Leave a comment


I’ve written a script in VBA in combination with IE to fill two input fields to login to a webpage.

When I try to execute my script, it throws Access is denied error because of the iframe within which the input fields are. How can I fill the two input fields with my credentials?

This is the site link

This is my try so far:

Sub LogIn()
    Const Url = "replace with above link"
    Dim IE As New InternetExplorer, HTML As HTMLDocument
    Dim frm As Object, post As Object

    With IE
        .Visible = True
        .navigate Url
        While .Busy Or .readyState < 4: DoEvents: Wend
        Set HTML = .document
    End With

    Application.Wait Now + TimeValue("00:00:05")

    Set frm = HTML.querySelector("iframe[name='disneyid-iframe']").contentWindow.document
    Set post = frm.querySelector("input[placeholder='Username or Email Address']")
    post.Value = "[email protected]"
End Sub

You can try with any input details. All I need to know is how can I fill the two boxes.

Html elements connected to iframe content:

<iframe id="disneyid-secure-responder" name="disneyid-secure-responder" src="https://www.espn.com/disneyid/responder/index.html?clientId=ESPN-ONESITE.WEB-PROD&amp;scheme=http&amp;postMessageOrigin=http%3A%2F%2Ffantasy.espn.com%2Fbasketball%2Fteam%3FleagueId%3D24874190%26teamId%3D1%26statSplit%3DcurrSeason&amp;cookieDomain=espn.com&amp;config=PROD&amp;logLevel=INFO&amp;topHost=fantasy.espn.com&amp;ageBand=ADULT&amp;countryCode=CA&amp;langPref=en-US&amp;cssOverride=https%3A%2F%2Fsecure.espncdn.com%2Fcombiner%2Fc%3Fcss%3Ddisneyid%2Fcore.css%2Cdisneyid%2Ffantasy.css&amp;responderPage=https%3A%2F%2Fwww.espn.com%2Fdisneyid%2Fresponder%2Findex.html&amp;buildId=165f40c7564" style="display: none;"></iframe>
How to&Answers:

As mentioned in the other answer, the iframe src is in a different domain and same domain policy effectively rules out IE for > version 8 in this case. Whilst messing with security settings to attempt to bypass this is a bad idea see this for reading.

This is easy enough with selenium basic for VBA using Chrome. You additionally need to loop until login details can be entered. I add a timeout to prevent an infinite loop.

After installing selenium you need to go VBE > Tools > References and add a reference to Selenium Type Library.

Option Explicit    
Public Sub Login()
    Dim d As WebDriver, ele As WebElement, t As Date
    Const MAX_WAIT_SEC As Long = 5
    Const url = "http://fantasy.espn.com/basketball/team?leagueId=24874190&teamId=1&statSplit=currSeason"
    Set d = New ChromeDriver

    With d
        .Start "Chrome"
        .get url
        t = Timer
            On Error Resume Next
            .SwitchToFrame .FindElementById("disneyid-iframe")
            Set ele = .FindElementByCss("[type=email]")
            On Error GoTo 0
            If Timer - t > MAX_WAIT_SEC Then Exit Do
        Loop While Not ele.IsDisplayed

        If ele Is Nothing Then Exit Sub

        ele.SendKeys "[email protected]"
        .FindElementByCss("[type=password]").SendKeys "password"

        Stop                                     '<== Delete me later
        'Other code
    End With
End Sub


Try to refer example below may solve your issue.

Main HTML Page Code (demo51.html):

<!DOCTYPE html>
<H2>This is main page...</h2><br>
<iframe src="C:\Users\Administrator\Desktop\demo52.html" id="ifrm" height="300px" width="300px">
  <p>Your browser does not support iframes.</p>


Page which will be displayed with in Main page Iframe (demo52.html).

<!DOCTYPE html>
<h2> Sample Page</h2><br>
Name: <input type="text" id="txt_name" value=""><br><br>
Age: &nbsp &nbsp<input type="text" id="txt_age" value=""><br><br>
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp <input type="submit" value="submit">

VBA Code:

Public Sub demo()

Dim baseURL As String
Dim IE As InternetExplorer
Dim HTMLdoc As HTMLDocument
Dim workFrame As HTMLIFrame
Dim acctInput As HTMLInputElement

baseURL = "C:\Users\Administrator\Desktop\demo51.html"

Set IE = New InternetExplorer

With IE
.Visible = True

.Navigate baseURL
While .Busy Or .ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend

Set workFrame = .Document.getElementById("ifrm")
Set HTMLdoc = workFrame.contentWindow.Document
End With

Set acctInput = HTMLdoc.getElementById("txt_name")
acctInput.Value = "abcde"

Set acctInput = HTMLdoc.getElementById("txt_age")
acctInput.Value = "25"

End Sub

Output in Internet Explorer:

enter image description here

Note: Try to add reference to library below in VBA Editor.

(1) Microsoft HTML Object Library.

(2) Microsoft Internet controls.

Try to change the URL in code as per your own requirement.


You cannot change things in an iframe on another domain due to security limitations.