PDA

View Full Version : Automate IE webpage booking



randomuser
04-24-2021, 08:33 AM
Hello,

I am trying to automate an online booking using Excel & VBA. I have little experience with VBA code. So far I am able to open a IE window, click the login drop down button, enter my username & password in the two fields and then click the login button. This process seems to be working fine, however when I get to the second page after login, I'm having trouble clicking another menu button to continue on in the booking. Here is my VBA code:



Sub GetIE()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "URL Of The Website" '


' Wait while IE loading...
Do
DoEvents
Loop Until IE.ReadyState = 4

'Clicks the SIGN IN button on the main menu - drop down window appears
IE.document.getElementById("sign-in-menu-btn").Click

'Enters the username into username field
IE.document.getElementById("Username").Value = "my_email_address" '

'Enters the Password into Password field
IE.document.getElementById("Password").Value = "my_password"


'clicks SIGN IN after login info
Set AllHyperLinks = IE.document.getElementsByTagName("A")
For Each hyper_link In AllHyperLinks

If hyper_link.href = "javascript:signIn()" Then
hyper_link.Click
Exit For
End If
Next hyper_link


End Sub




The next button that I am trying to click has the HTML code of this:



<a class="anchorbutton gray vertical" onclick="this.blur();" href="example_link">Book a Tee Time</a>


Thanks for any assistance.

SamT
04-24-2021, 08:45 AM
Why Click? After Sign In, repeat the Wait loop, then IE.Navigate "URL Example_Link".

randomuser
04-24-2021, 02:07 PM
Thanks, that worked. I added another wait loop and then copied the new URL link.
Is there a way to open the link in the same internet explorer window?

Here is the next section of code I used:



' Wait while IE loading...
Do
DoEvents
Loop Until IE.ReadyState = 4


Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "URL of second page "

randomuser
04-24-2021, 05:24 PM
I am also having trouble trying to click a Div Class ID.


I have tried this code, but get the Runtime error 424 (Object Required):


IE.document.getElementById("PRGEF2021-05-02-08.33.00.00018").Click


Here is the HTML of the CSS button I am trying to click:


<div class="search-results-tee-times-box eighteen-holes PRGE-box" id="PRGEF2021-05-02-08.33.00.00018" style="height: 153.3px;" onclick="isNotLocked('PRGE', 'F', '2021-05-02', '08:33', 18, 1, function() { document.getElementById('Players').value='';loadGenericMessage102Popup(func tion() { unlockBooking('PRGE', 'F', '2021-05-02', '08:33') }, getPopupTop('PRGEF2021-05-02-08.33.00.00018') );scrollSize102(); }, false, 'PRGEF2021-05-02-08.33.00.00018')">
<p class="time">
8:33<span class="am-pm">am</span>
</p>
<p class="price"></p>
<p class="item-description-PRGE item-description" style="height: 0px;"></p>
<a class="anchorbutton players-holes-button-50" href="javascript:void(0)">
<div class="players-allowed">
1 Player
</div>
<div class="booking-holes">18 Holes</div>
<div class="right-arrow">►</div>
</a>
<script type="text/javascript">
document.getElementById("PRGEF2021-05-02-08.33.00.00018").setAttribute("onclick", "isNotLocked('PRGE', 'F', '2021-05-02', '08:33', 18, 1, function() { document.getElementById('Players').value='';loadGenericMessage102Popup(func tion() { unlockBooking('PRGE', 'F', '2021-05-02', '08:33') }, getPopupTop('PRGEF2021-05-02-08.33.00.00018') );scrollSize102(); }, false, 'PRGEF2021-05-02-08.33.00.00018')");
</script>
</div>

SamT
04-24-2021, 06:07 PM
Is there a way to open the link in the same internet explorer window?
I would think that you just reuse the original object:
' Wait while IE loading...
Do
DoEvents
Loop Until IE.ReadyState = 4
IE.navigate "URL of second page"

I haven't done any HTM coding in a dozen years and have never used JavaScript. I imagine something like

document.getElementById("PRGEF2021-05-02-08.33.00.00018").Click

Rememeber, you're using VBA to operate on (IE) Objects modifed by js, so it might be as simple as

IE.Elements("PRGEF2021-05-02-08.33.00.00018").Click
OTOH, you may have to set the Element's Attributes before "clicking".
See JavaScript For Dummies (https://duckduckgo.com/?q=JavaScript+For+Dummies&t=ffcm&ia=web) and the IE VBA Object model (https://duckduckgo.com/?q=IE+VBA+Object+model&t=ffcm&ia=web)