input data into div on login page - excel

https://imgur.com/a/QauSk1H
Set IE = New InternetExplorer
IE.Visible = True
IE.navigate "https://mdemo.cqg.com/cqg/desktop/logon"
Do While IE.Busy = True: DoEvents: Loop
Set HTMLDoc = IE.document
Set wpfe = HTMLDoc.getElementsByClassName("wpfe-logon-input-full wpfe-native-faded-input ng-untouched ng-pristine ng-valid")(0)
wpfe.Value = "User Login"
I'm tyring to enter login but it looks like emm... nothing good. Screens of interesting code part and error attached at the top. I hope that you might be able to help.

You want to get the first input field by the css class names "wpfe-logon-input-full wpfe-native-faded-input ng-untouched ng-pristine ng-valid". I wrote names because every string seperated by a space is an own css class name. The last css class name is not ng-valid when you load the page. It's ng-invalid. That's the reason for your error.
The solution is to use only the first css class wpfe-logon-input-full. But that's not enough to get your goal.
After the first html code was loaded and the IE reports the browser is no more busy, the IE lies to you ;-) There is dynamic content which will be load after that. So you have to wait till that's done.
But even then the login still does not work. You have to trigger two html events per input field.
This code works:
Sub Login()
Const url As String = "https://mdemo.cqg.com/cqg/desktop/logon"
Dim ie As Object
Dim HTMLDoc As Object
Dim nodeUsername As Object
Dim nodePassword As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate url
Do While ie.Busy = True: DoEvents: Loop
Application.Wait (Now + TimeSerial(0, 0, 5))
Set HTMLDoc = ie.document
Set nodeUsername = HTMLDoc.getElementsByClassName("wpfe-logon-input-full")(0)
Call TriggerEvent(HTMLDoc, nodeUsername, "compositionstart")
nodeUsername.Value = "TestName"
Call TriggerEvent(HTMLDoc, nodeUsername, "compositionend")
Set nodePassword = HTMLDoc.getElementsByClassName("wpfe-logon-input-full")(1)
Call TriggerEvent(HTMLDoc, nodePassword, "compositionstart")
nodePassword.Value = "TestPassword"
Call TriggerEvent(HTMLDoc, nodePassword, "compositionend")
HTMLDoc.getElementByID("login").Click
End Sub
Use this procedure to trigger html events:
Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)
Dim theEvent As Object
htmlElementWithEvent.Focus
Set theEvent = htmlDocument.createEvent("HTMLEvents")
theEvent.initEvent eventType, True, False
htmlElementWithEvent.dispatchEvent theEvent
End Sub

Related

Click on webpage button function doesnt work in excel macro

I'm trying to create a fedex tracking automation but I'm unable to get the Track button clicked. any idea please?
Sub Fedex()
Dim IE As Object
Dim doc As HTMLDocument
Set IE = New InternetExplorerMedium
IE.Visible = True
IE.navigate "https://www.fedex.com/en-us/home.html"
Do While IE.Busy Or IE.READYSTATE = 4
Loop
Application.Wait (Now + TimeValue("0:00:07"))
Set searchbx = IE.document.getElementsByName("trackingnumber")(0)
searchbx.Value = "Howdy!"
IE.document.getElementById("btnSingleTrack").Click
End Sub
Edit - Do it direct via url parameter
You can manipulate the url you navigate. Look at the following example. Change the value of the last parameter YourTrackingNumberHere to a working tracking number and test it manually:
https://www.fedex.com/apps/fedextrack/?action=track&cntry_code=us&locale=en_US&trackingnumber=YourTrackingNumberHere
First answer to make it work in your way
The search box has events. You must trigger the change event after setting your tracking number. I never seen it before, but after triggering the change event, you must wait a few seconds to make the button click work.
Sub Fedex()
Dim ie As Object
Dim searchBox As Object
'Initialize Internet Explorer, set visibility,
'call URL and wait until page is fully loaded
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "https://www.fedex.com/en-us/home.html"
Do While ie.READYSTATE <> 4: DoEvents: Loop
Set searchBox = ie.document.getElementsByName("trackingnumber")(0)
searchBox.Value = "Howdy!"
'Trigger the change event of the input box
Call TriggerEvent(ie.document, searchBox, "change")
'I don't know why, but you have to wait a few seconds
'Otherwise the button click will not work
'In my tests, I need 5 seconds to make it work in every case
'You can make your own tests for your environment
Application.Wait (Now + TimeValue("0:00:05"))
ie.document.getElementById("btnSingleTrack").Click
End Sub
With this procedure you can trigger every event:
Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)
Dim theEvent As Object
htmlElementWithEvent.Focus
Set theEvent = htmlDocument.createEvent("HTMLEvents")
theEvent.initEvent eventType, True, False
htmlElementWithEvent.dispatchEvent theEvent
End Sub

HTML Doc not match with Get element

iam trying accessing HTML doc from this site
https://script.google.com/macros/s/AKfycbwIE5ZwronuD81Ze5yu7DTVeaRRiNYZI6wX62WED3ix/dev
and iam using this code in my excel vba to inspect inner HTML or outer HTML
Dim MyBrowser As New InternetExplorer
If MyBrowser Is Nothing Then
Set MyBrowser = New InternetExplorer
End If
myUrl = "https://script.google.com/macros/s/AKfycbwIE5ZwronuD81Ze5yu7DTVeaRRiNYZI6wX62WED3ix/dev"
MyBrowser.Visible = True
MyBrowser.navigate myUrl
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Application.Wait DateAdd("s", 3, Now)
Set myhttp = MyBrowser.document
Set myObj = myhttp.DocumentElement
Debug.Print myObj.outerHTML
and the result is
is totally different when i am doing inspect element from the browser. I can found all id for all textbox that i want to..any suggest or explanation why become like this.
many thanks for all help..

Excel VBA .click event not working with <a> tag

I'm trying to automate logging into a website to get my account balance.
The .click event on the login button is not working. I feel I'm missing something simple.
Sub test()
Set IE = CreateObject("InternetExplorer.Application")
my_url = "https://www.synchronycredit.com/eSecurity/Login/login.action?clientId=google&accountType=plcc&langId=en"
With IE
.Visible = True
.navigate my_url
.Top = 50
.Left = 530
.Height = 400
.Width = 400
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
End With
IE.document.getElementById("loginUserID").Value = "userNameHere"
IE.document.getElementById("loginPassword").Value = "passWordHere"
IE.document.getElementByID("secLoginBtn").Click
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
End Sub
Here is the code from the website:
<a tabindex="0" title="Login to your account" id="secLoginBtn" role="button" style="cursor: pointer;" data-reason="login:start:submit" onclick="javascript:setRememberMeFlag('rememUID');" data-type="button" class="ADAbtn btn secLoginBtn secureLockImg ensightenmEvent">
<img class="pull-left" src="/essimages/cs/groups/ess_webasset/generic/#common/en/brandimages/044828.png" alt="">
Secure Login</a>
First of all:
Always use Option Explicit as first line in every VBA module.
To solve your problem you need first to trigger the keypress events for the input fields. After entering the values you can click the submit button.
First line:
Option Explicit
Main makro:
Sub test()
Dim browser As Object
Dim url As String
Dim nodeUserName As Object
Dim nodePassWord As Object
Dim nodeSubmitButton As Object
Dim domDoc As Object
Dim userName As String
Dim passWord As String
userName = "Your Name"
passWord = "YourPassword"
Set browser = CreateObject("InternetExplorer.Application")
url = "https://www.synchronycredit.com/eSecurity/Login/login.action?clientId=google&accountType=plcc&langId=en"
'Initialize Internet Explorer, set visibility,
'call URL and wait until page is fully loaded
Set browser = CreateObject("internetexplorer.application")
browser.Visible = True
browser.navigate url
browser.Top = 50
browser.Left = 530
browser.Height = 400
browser.Width = 400
Do Until browser.readyState = 4: DoEvents: Loop
Set domDoc = browser.document
Set nodeUserName = domDoc.getElementByID("loginUserID")
Call TriggerEvent(domDoc, nodeUserName, "onKeypress")
nodeUserName.Value = userName
Set nodePassWord = domDoc.getElementByID("loginPassword")
Call TriggerEvent(domDoc, nodePassWord, "onKeypress")
nodePassWord.Value = passWord
Set nodeSubmitButton = domDoc.getElementByID("secLoginBtn")
nodeSubmitButton.Click
End Sub
Subroutine to trigger a html event:
Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)
Dim theEvent As Object
htmlElementWithEvent.Focus
Set theEvent = htmlDocument.createEvent("HTMLEvents")
theEvent.initEvent eventType, True, False
htmlElementWithEvent.dispatchEvent theEvent
End Sub

VBA: Go to IE site and tick by element

New to navigating with IE. I m trying to go to a site https://www.thetrainline.com/ and tick the Return button by using element getElementsByID("return").FirstChild.Click However all this does is bring up the site. It doesn't tick the return option.
Any ideas?
Public Function GetHTML() As String
On Error GoTo ErrorEscape
Dim URL As String
Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application")
Dim HTML As Object
Dim myElement As Object
'Define URL
URL = "https://www.thetrainline.com/"
IE.navigate URL
Application.Wait (Now + TimeValue("0:00:03"))
MsgBox ("IE Loaded")
Set myElement = IE.getElementsByID("return").FirstChild.Click
Exit Function
ErrorEscape:
Set IE = Nothing
Set HTML = Nothing
End Function
P.S. I want many users to be able to use this without having to download something. Hence, why I am not using Selenium.
I try to modify your code to click on 'return' radio button.
Modified code:
Sub demo()
Dim URL As String
Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application")
Dim HTML As Object
Dim myElement As Object
'Define URL
URL = "https://www.thetrainline.com/"
IE.Visible = True
IE.navigate URL
Application.Wait (Now + TimeValue("0:00:03"))
'MsgBox ("IE Loaded")
IE.document.getElementById("return").Click
ErrorEscape:
Set IE = Nothing
Set HTML = Nothing
End Sub
Output:

Extract html source code into excel using VBA

I am trying to simply paste the content or innertext into excel using getElementByID function.
The content is actually the iframe link which I am trying to extract it and paste into cell.
The photo shown is the html source code.
Sub GetData()
Dim ie As New SHDocVw.InternetExplorer
Dim htmldoc As MSHTML.HTMLDocument
Dim result As MSHTML.IHTMLElement
ie.Visible = True
ie.navigate "http://www.bursamalaysia.com/market/listed-companies/company-announcements/5925865"
Do While ie.readyState <> READYSTATE_COMPLETE
Loop
Application.Wait (Now() + TimeValue("00:00:016")) ' For internal page refresh or loading
Set htmldoc = ie.document
Set Results = HTML.getElementById("bm_ann_detail_iframe")
Sheets("Sheet1").Range("a1").Value = Results.innerText
End Sub
html source code
You should use consistent variable naming in your code. If you put Option Explicit at the top of your code that will help.
You want to access the src attribute of the iframe to get the URL shown.
If you plan to use the new URL then you actually want the part before the "#". This means changing to:
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = Split(ie.document.getElementById("bm_ann_detail_iframe").src, "#")(0)
Code:
Option Explicit
Public Sub GetData()
Dim ie As New SHDocVw.InternetExplorer
ie.Visible = True
ie.navigate "http://www.bursamalaysia.com/market/listed-companies/company-announcements/5925865"
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
ThisWorkbook.Worksheets("Sheet1") = ie.document.getElementById("bm_ann_detail_iframe").src
ie.Quit
End Sub

Resources