Opening a web site and clicking a button

KitaYama

Well-known member
Local time
Today, 10:43
Joined
Jan 6, 2022
Messages
1,756
I have an image in a form that clicking it, opens a site in default browser.
The page is a login page that contains only two textboxes and a button.
Is there anyway to click the button without user's interaction (by vba or any other way)?

I know that clicking a button is not that hard, but I've seen @Edgar_ or others have been successful in some web interactions via vba.
I simply thought reducing a step, can be a help too.

thanks.
 
Are you already able to fill in the boxes on the webinar using VBA? If so, you could try either calling the Click event of that button or call the function it's calling using JS syntax.
 
I have done this only once. Example of old code:
Code:
        'open ASTM/AASHTO test standards website and pass agency username/password to the web page
        Dim oBrowser As InternetExplorer
        Set oBrowser = New InternetExplorer
        oBrowser.Silent = True
        oBrowser.Navigate "https://login.ihserc.com/login/erc?"
        oBrowser.Visible = True
        Do
            'Wait till the Browser is loaded
        Loop Until oBrowser.ReadyState = READYSTATE_COMPLETE
        oBrowser.Document.all.Item("subAcctLoginName").Value = "name"
        oBrowser.Document.all.Item("subAcctPassword").Value = "password"
        oBrowser.Document.all.Item("Submit").Click

I have no idea how to do this with Edge or any other browser.
 
Is there anyway to click the button without user's interaction (by vba or any other way)?
A browser extension could detect you're opening a particular site and then click the button. You could even make the browser extension wait until the textboxes are filled to click it. You can't write a browser extension with VBA, though.

Another possibility that does let you do this with VBA is the user32 library of Windows. You could use the SetCursorPos function to move the mouse to the location of the button and then send a click with the mouse_event function. It depends on the screen resolution and the time it takes the site to load.

There is also the possibility of having a bookmarklet click the button, but that requires the action of clicking the bookmarklet first. Many other instructions could be included there, though, so it would actually be useful if you had to do many other additional things. If you only need to click that button, you could do it directly without the bookmark (unless there is something not letting you click it directly).

If by clicking the image, instead of opening the default browser, you open an IE application, then you totally can click it. It requires the IE instance. It's not the default browser (I guess you're not using IE to browse the internet nowadays).

If by clicking the image, instead of opening the default browser, you open an automatable browser such as Chromium (if using puppeteer) or any of the web drivers of Selenium, then you totally can click it too.

I might be forgetting a few other methods, but basically, manipulating the content of a website requires your to have control of the browser. Just opening the default browser does not give you control on it. If I recall correctly, you can take control of IE even if you did not open it from VBA, but other browsers do not let you unless you open an automatable version of them.

Additionally, you can automate a browser from within using its console. Like, for instance, I'm writing this from Firefox, if I click F12 > console and then I write alert("hi mom") and hit enter, I will see the message.
 
Are you already able to fill in the boxes on the webinar using VBA? If so, you could try either calling the Click event of that button or call the function it's calling using JS syntax.
Browser fills the textboxes.
thanks.
 
A browser extension could detect you're opening a particular site and then click the button. You could even make the browser extension wait until the textboxes are filled to click it. You can't write a browser extension with VBA, though.

Another possibility that does let you do this with VBA is the user32 library of Windows. You could use the SetCursorPos function to move the mouse to the location of the button and then send a click with the mouse_event function. It depends on the screen resolution and the time it takes the site to load.

There is also the possibility of having a bookmarklet click the button, but that requires the action of clicking the bookmarklet first. Many other instructions could be included there, though, so it would actually be useful if you had to do many other additional things. If you only need to click that button, you could do it directly without the bookmark (unless there is something not letting you click it directly).

If by clicking the image, instead of opening the default browser, you open an IE application, then you totally can click it. It requires the IE instance. It's not the default browser (I guess you're not using IE to browse the internet nowadays).

If by clicking the image, instead of opening the default browser, you open an automatable browser such as Chromium (if using puppeteer) or any of the web drivers of Selenium, then you totally can click it too.

I might be forgetting a few other methods, but basically, manipulating the content of a website requires your to have control of the browser. Just opening the default browser does not give you control on it. If I recall correctly, you can take control of IE even if you did not open it from VBA, but other browsers do not let you unless you open an automatable version of them.

Additionally, you can automate a browser from within using its console. Like, for instance, I'm writing this from Firefox, if I click F12 > console and then I write alert("hi mom") and hit enter, I will see the message.
Our users are using either Edge or Chrome. But if necessary, it would be OK if IE opens the page (Is it available still in Windows 11? I have to check it)
I'll appreciate any kind of help on either (or both) of these browsers.
Meanwhile, I'll check the code presented by @June7 to see if I have any luck.

Thanks.
 
I have done this only once. Example of old code:
Code:
        'open ASTM/AASHTO test standards website and pass agency username/password to the web page
        Dim oBrowser As InternetExplorer
        Set oBrowser = New InternetExplorer
        oBrowser.Silent = True
        oBrowser.Navigate "https://login.ihserc.com/login/erc?"
        oBrowser.Visible = True
        Do
            'Wait till the Browser is loaded
        Loop Until oBrowser.ReadyState = READYSTATE_COMPLETE
        oBrowser.Document.all.Item("subAcctLoginName").Value = "name"
        oBrowser.Document.all.Item("subAcctPassword").Value = "password"
        oBrowser.Document.all.Item("Submit").Click

I have no idea how to do this with Edge or any other browser.
Which reference do I have to add to prevent "User-defined type not defined) error.
Windows 11, Office 365.

Thanks.
 
Add a reference to Microsoft Internet Controls, or convert to Object and use CreateObject("InternetExplorer.Application") for the instances.

Paste the html code of the website here to see how you can automate it or give us some URL
 
Adding the reference to the code offered by @June7 did the job.
The site opens and I can login without clicking.

Now I have a problem. If possible, I prefer to use Edge or Chrome because IE has been abolished in Win11.
Is there any code for these (any of them or both)?

The site is a government site and I'm not allowed to share the url.
A part of the source code (containing the form is here).

I really appreciate your and @June7 help.

HTML:
<td width="60%" align="center" valign="top">
      <img src="image/****.png" width="100%"><br><br><br>
      <table bgcolor="#FFFF99"><tbody><tr><td width="640" height="70">
        <div align="center"><font size="4">
          <b><span class="css4">Some local language text here about the form。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></b>
          <br>
          <b><span class="css4">Some local language text here about the login 。</span></b>
        </font></div>
      </td></tr></tbody></table>
      <br>
      <form name="frm_login" action="servlet/base.sup.S_TOP_Login" method="post">
        <table>
          <tbody><tr>
            <td nowrap="">
              <b><font size="3" class="css3">User ID (text in local language)</font></b>
            </td>
            <td>
              <input size="20" onmouseover="window.status='User ID (text in local language)'" maxlength="12" onmouseout="window.status=''" name="USER_ID" value="">
            <div data-lastpass-icon-root="" style="position: relative !important; height: 0px !important; width: 0px !important; float: left !important;"></div></td>
          </tr>
          <tr>
            <td nowrap="">
              <b><font size="3" class="css3">Password: (text in local language)</font></b>
            </td>
            <td>
              <input type="password" size="20" maxlength="30" onmouseover="window.status='Password: (text in local language)'" onmouseout="window.status=''" name="PASS_WD" value="">
            <div data-lastpass-icon-root="" style="position: relative !important; height: 0px !important; width: 0px !important; float: left !important;"></div></td>
          </tr>
        </tbody></table>
        <p>
          <input type="button" name="Login_Button" value="Login: (text in local language)" onclick="login()">
        </p>
      </form>
    </td>
 
You can download selenium. Caution: it's tricky.

First step is to get SeleniumBasic by going here:
https://github.com/florentbr/SeleniumBasic/releases/tag/v2.0.9.0
Download only the executable and install it

Then, check what version of Chrome you have. Open chrome normally, then check the hamburger menu (the three vertical dots) on the corner and go to help > about, something like that, just find the version. My version is 109.xxx, yours should be something like that or higher (or lower).

Then, go here:
https://chromedriver.storage.googleapis.com/index.html
Look for your version and download your driver. Once downloaded, you have to paste the files into the SeleniumBasic directory. Mine was this:
C:\Users\Edgar\AppData\Local\SeleniumBasic

If you have versions superior to 115, then you need to do yet another tricky thing.

Here's how the code could look:
Code:
Public Sub ClickDamnButton()
    Dim driver As New WebDriver
    Dim btn As WebElement

    driver.Start "chrome"
    driver.Get url

    ' Find the element
    Set btn = driver.FindElementByName("Login_Button")
    btn.Click

    ' Prevent premature close
    Do: DoEvents: Loop

End Sub
 
Last edited:
By the way, you can do this button clicking with a browser control too, I knew I had forgotten something important in post #4
 
Thanks to all.
I think I will work on it this evening and see how it goes.

Million thanks.
 

Users who are viewing this thread

Back
Top Bottom