I am VERY new to VBA and have been watching a ridiculous amount of youtube videos (specifically wise owl tutorials) and have searched here to try and find the answer to my question. I am writing a code to automate part of my job. I have the code to log in to a site that I can't share because I work with classified docs but I will share what I can. Here is the code so far...
Sub Login_and_click_link()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLDiv As MSHTML.IHTMLElement
Dim HTMLAs As MSHTML.IHTMLElementCollection
Dim HTMLA As MSHTML.IHTMLAnchorElement
'Open Internet Explorer and Navigate to PDDA
With IE
.Visible = True
.Navigate "http://.TheWebsiteINeed.com"
End With
'Loop until PDDA page has fully loaded
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
'Search DOM for element by ID and input username
Set HTMLDoc = IE.Document
Set HTMLInput = HTMLDoc.getElementById("userName")
HTMLInput.Value = Worksheets("sheet1").Range("letternumber").Text
'Search DOM for an element by ID and input password
Set HTMLInput = HTMLDoc.getElementById("Password")
HTMLInput.Value = Worksheets("sheet1").Range("letternumber").Text
'Search DOM for element by ID and click submit
Set HTMLInput = HTMLDoc.getElementById("submit")
HTMLInput.Click
'Navigate to the tab I need
??????
End Sub
There are two links on the page, each will take me to the same page that I need next. So whichever is easier to write code for you can use.
The link at the top is this(within a tr tag):
<span> class="topNavoff" style="cursor: hand;" onmouseover="window.status='Process
Management';this.className='topNavon';"
onmouseout="window.status='';this.className='topNavoff';"
onclick="setTask('pmMain');">Process</span>
The link at the bottom is this (within a p tag):
<span> class="contentSubtitleLink" id="process" style="cursor: hand;"
onmouseover="window.status='Process Management';" onmouseout="window.status='';"
onclick="setTask('pmMain');">Process</span>
Let me know if there is anything I can send to help make this more clear. ANY help would be GREATLY appreciated!
i want to replace the line htmldoc from htmlobject library to something suitable for selenium. i want to pass htmldoc as argument in another subroutine so Here is the code:
Dim htmldoc As MSHTML.HTMLDocument
Dim htmldiv As Selenium.WebElement
Dim htmlul As Selenium.WebElement
Dim htmlAs As Selenium.WebElements
Dim htmlA As Selenium.WebElement
Dim TableName As String
URL = "https://www.whoscored.com/Statistics"
sel.Start "Chrome"
sel.Get URL
'set htmldoc= sel.document..... something....
Set htmldiv = sel.FindElementById("top-player-stats")
Set htmlul = sel.FindElementById("top-player-stats-options")
Set htmlAs = htmlul.FindElementsByTag("a")
For Each htmlA In htmlAs
TableName = htmlA.attribute("href")
htmlA.Click
GoToTable htmldoc, TableName
Next htmlA
End Sub
If you're trying to capture the entire HTML source code.
One options is to use
sel.PageSource
But that might not behave as you expect as a limitation to how it is generated (source: https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/WebDriver.html#getPageSource()).
You could also try these after the page is fully loaded:
sel.ExecuteScript("return document.documentElement.innerHTML")
sel.ExecuteScript("return document.body.innerHTML")
I'm a beginner in VBA and I've failed to select country name automatically in web Combo box or list box from my Excel spreadsheet. My code is entering country name only, but not selecting it.
How can I change this code so it can pick country name from my Excel spreadsheet and select the same in web combo box as a loop. Passport number, DOB and Nationality are correct on my code. If you'll use manually then you can find the work permit number which I need to capture in my spreadsheet. Chrome Inspect Element screenshot is attached herewith.
My code is as follows:
Sub MOL()
Dim IE As New SHDocVw.InternetExplorer
Dim Doc As MSHTML.HTMLDocument
Dim Buttons As MSHTML.IHTMLElementCollection
Dim Button As MSHTML.IHTMLElement
Dim HTMLInput As MSHTML.IHTMLElement
Dim Tags As MSHTML.IHTMLElement
Dim HTMLTables As MSHTML.IHTMLElementCollection
Dim HTMLTable As MSHTML.IHTMLElement
Dim HTMLRow As MSHTML.IHTMLElement
Dim HTMLCell As MSHTML.IHTMLElement
Dim Alltext As IHTMLElementCollection
Application.ScreenUpdating = False
'Application.Calculation = xlCalculationManual
'Application.EnableEvents = False
On Error Resume Next
IE.Visible = True
IE.navigate "https://eservices.mol.gov.ae/SmartTasheel/Complain/IndexLogin?lang=en-gb"
Do While IE.readyState <> READYSTATE_COMPLETE: Loop
Set Doc = IE.document
Set Buttons = Doc.getElementsByTagName("Button")
Buttons(2).Click
Do While IE.readyState <> READYSTATE_INTERACTIVE = 3: Loop
Set HTMLInputs = Doc.getElementsByTagName("Input")
HTMLInputs(46).Value = "somevalue"
HTMLInputs(48).Value = "24/02/1990"
HTMLInputs(47).Value = "India"
Buttons(21).Click
End Sub
The solution you look for is a bit difficult to provide. There are few tricky parts to hurdle to select the NATIONALITY from dropdown. I've used .querySelector() within the script to make it concise. However, it should serve your purpose no matter whatever country you wanna select from dropdown. Give it a shot:
Sub GetInfo()
Dim IE As New InternetExplorer, HTML As HTMLDocument, post As Object, URL$
URL = "https://eservices.mol.gov.ae/SmartTasheel/Complain/IndexLogin?lang=en-gb"
With IE
.Visible = True
.navigate URL
While .Busy = True Or .readyState < 4: DoEvents: Wend
Set HTML = .document
HTML.getElementById("TransactionInfo_WorkPermitNumber").innerText = "2659558"
HTML.querySelector("button[ng-click='showEmployeeSearch()']").Click
Application.Wait Now + TimeValue("00:00:03") ''If for some reason the script fails, make sure to increase the delay
HTML.getElementById("txtPassportNumber").Value = "J2659558"
HTML.getElementById("Nationality").Focus
For Each post In HTML.getElementsByClassName("ng-scope")
With post.getElementsByClassName("ng-binding")
For I = 0 To .Length - 1
If .item(I).innerText = "INDIA" Then ''you can change the country name here to select from dropdown
.item(I).Click
Exit For
End If
Next I
End With
Next post
HTML.getElementById("txtBirthDate").Value = "24/02/1990"
HTML.querySelector("button[onclick='SearchEmployee()']").Click
End With
End Sub
Reference to add to the library:
Microsoft Internet Controls
Microsoft HTML Object library
When you execute the above script, it should give you the desired result.
Another way would be to go for using xmlhttp request which is way faster than IE. You need to pass the query string parameter arguments as dictionary through "POST" request. If you want to change the parameter as in, birth date,passportor nationality just do it in the QueryString. Btw, the Nationality parameter should be filled in with value instead of name as in, 100 for INDIA. This is how your script should look like:
Sub Get_Data()
Dim res As Variant, QueryString$, ID$, Name$
QueryString = "{""PersonPassportNumber"":""J2659558"",""PersonNationality"":""100"",""PersonBirthDate"":""24/02/1990""}"
With New XMLHTTP
.Open "POST", "https://eservices.mol.gov.ae/SmartTasheel/Dashboard/GetEmployees", False
.setRequestHeader "User-Agent", "Mozilla/5.0"
.setRequestHeader "Content-Type", "application/json"
.send QueryString
res = .responseText
End With
ID = Split(Split(Split(res, "Employees"":")(1), "ID"":""")(1), """,")(0)
Name = Split(Split(Split(res, "Employees"":")(1), "OtherData2"":""")(1), """}")(0)
[A1] = ID: [B1] = Name
End Sub
Reference to add to the library:
Microsoft XML, V6.0
Running the above script, you should get the NAME and ID of your required search.
I have been looking for the answer on this question for quite some time. Below here i have two parts of code that load html of a website into memory. Same result. But de getelements methods, for example getelementsbyclassname do not work when i use the 'Get' method. I wanted to use the quicker 'Get' method but due to this different outcome I couldn't. In the first line of code getElementsByClassName works, but in the second part its outcome remains nothing.
I really couldn't figure out why, I have been stuck for some time now. I hope here you can help met out. Thank you in advance.
<i>Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim URL As String
Dim Element1 As MSHTML.IHTMLElement, Element2 As MSHTML.IHTMLElement,
Element3 As MSHTML.IHTMLElement
Dim Elementen As MSHTML.IHTMLElementCollection
URL = "https://www.google.nl/?gfe_rd=cr&dcr=0&ei=KXNcWsHNJ9OB4gTcjqvwCA"
IE.Visible = True
IE.navigate URL
Do While IE.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
Set HTMLDoc = IE.document
Set Element1 = HTMLDoc.getElementsByClassName("gsfi")(0)
Set Element2 = HTMLDoc.getElementById("lst-ib")
Debug.Print Element1.className, Element2.className
Dim XMLPage As New MSXML2.XMLHTTP60
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim URL As String
Dim Element1 As MSHTML.IHTMLElement
Dim Element2 As MSHTML.IHTMLElement
URL = "https://www.google.nl/?gfe_rd=cr&dcr=0&ei=KXNcWsHNJ9OB4gTcjqvwCA"
XMLPage.Open "Get", URL, False
XMLPage.send
HTMLDoc.body.innerHTML = XMLPage.responseText
Set Element1 = HTMLDoc.getElementsByClassName("gsfi")(0)
Set Element2 = HTMLDoc.getElementById("lst-ib")
Debug.Print Element2.className</i>
During exectuion of below mentioned code,following error appears."Object does not support this property of Method".Any suggestion why my statement does not work?
I appreciate your replies!
Sub Getdata()
Dim Ie As New InternetExplorer
Dim WebURL
Dim Docx As HTMLDocument
Dim productDesc1
Dim productTitle
Dim price
Dim RcdNum
Ie.Visible = False
WebURL = "http://www.amazon.com/Pyramex-I-Force-Sporty-Anti-Fog-Goggle/dp/B006WPSDXS/ref=sr_1_1?s=sporting-goods&ie=UTF8&qid=1455101600&sr=1-1&keywords=glassess"
Ie.Navigate2 WebURL
Do Until Ie.ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
Set Docx = Ie.Document
productDesc1 = Docx.window.frames("product-description-iframe").contentWindow.document.getElementsByClassName("productDescriptionWrapper").innerText
End Sub
getElementsByClassName("productDescriptionWrapper") will return a collection of elements (even if there's only one found).
That collection deosn't have an innerText property - you need to use something like:
productDesc1 = Docx.frames("product-description-iframe"). _
document.getElementsByClassName( _
"productDescriptionWrapper")(0).innerText
EDIT: made some changes - tested and works for me