Get cookie from InternetExplorer object - vba

I'm working a VBA project uses the following code to automate IE. How can I able to get cookies from current IE session?
Sub IE()
Dim objIE As InternetExplorer
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
objIE.Navigate "targeturl"
While objIE.Busy
DoEvents
Wend
getCookie = objIE.Cookie
End Sub

getCookie = objIE.document.Cookie

Related

VBA WebScraping a website that requires sendkeys

I need to webscrape a website that I actually have to type the keys to show the password form.
I've tried this code with no success (even using the right login)
Sub login()
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate ("https://wsvr10.coamo.com.br/npl/#!/login")
While IE.readyState <> READYSTATE_COMPLETE
Wend
sng = Timer
Do While sng + 5 > Timer
Loop
Dim idoc As MSHTML.HTMLDocument
Set idoc = IE.document
idoc.all.cpf.Value = "11111111111"

i am writing vba code to scrape data from web, I got error of Remote server Machine does not exist or is unavialable

Sub Launch_website()
Dim ie As SHDocVw.InternetExplorer
Dim myurl As String
Set ie = New SHDocVw.InternetExplorer
ie.Visible = True
myurl = "https://www.lifeinscouncil.org/industry%20information/NewBusinessPerformance"
'ie.Navigate "https://www.lifeinscouncil.org/industry%20information/NewBusinessPerformance" '
ie.Navigate myurl
'On Error Resume Next
Do While ie.ReadyState <> READYSTATE_COMPLETE
Loop

How to switch from IE to XMLHTTP60

I need to switch from ie to xmhttp60, and get the result of the execScript. Is there any way to do it? Below the code
I'm using.
Sub MostrarMasButton()
Dim ie As InternetExplorer
Dim CurrentWindow As HTMLWindowProxy
url = "https://es.investing.com/equities/amazon-com-inc-earnings"
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate url
Do While ie.Busy = True Or ie.readyState <> 4: DoEvents: Loop
ie.document.parentWindow.execScript ("showMoreEarningsHistory(6435, this)")
End Sub

Clicking a button in Internet Explorer

I am trying to create some VBA code that clicks a button with the following HTML:
<button class="button small btnViewAction atlas--ui-button" type="button" data-tracking-value="Action" data-tracking-label="View Action" data-tracking-## Heading ##category="Reconciliations" data-attribs-childassignmentid="661" data-attribs-reconciliationid="145870" data-attribs-assignmenttype="A" data-attribs-assignmentid="1223" value="undefined" data-columnname="action">Edit</button>
Is there a way to reference this button?
Without seeing more of the HTML I can't say this will work for sure, but it should give you a good guideline for what you can do!
Make sure you update ie.navigate to your site.
This is a late binding example, you can also set a reference to Microsoft Internet Controls.
Sub clickButton()
Dim ie As Object
Dim Btn As Object
'Late Binding
Set ie = CreateObject("InternetExplorer.Application")
On Error GoTo Catch
ie.Visible = True
ie.navigate "https://yourwebsite.com/"
While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
'LOOP EACH CLASS ELEMENT
For Each Btn In ie.Document.getElementsByClassName("button")
If Btn.innertext = "Edit" Then
Btn.Click
End If
Next Btn
'CLOSE INSTANCE OF IE
Catch:
ie.Quit
Set ie = Nothing
End Sub
Here is a very generic sample of how to do this.
Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Login_2_Website()
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "https://www.google.com/accounts/Login"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True
Do
' Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = oBrowser.Document
HTMLDoc.all.Email.Value = "sample#vbadud.com"
HTMLDoc.all.passwd.Value = "*****"
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Debug.Assert Err = 0
Err.Clear
Resume Next
End If
End Sub
Try the code above and see if you can implement it for your specific situation. If you need a nudge, post back, with additional requirements.

How to select values from Dropdown menu on a webpage:VBA

My code :
Sub login()
Dim IE As Object
Dim HTMLDoc As Object, HTMLDoc2 As Object
Dim objCollection As Object
Const navOpenInNewTab = &H800
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https:/com/1/19/login.esp"
Do While IE.Busy Or IE.ReadyState <> 4: Loop
Set HTMLDoc = IE.Document
With HTMLDoc
HTMLDoc.getElementById("USERNAME").Value = "xxxx" 'Entering credential
HTMLDoc.getElementById("PASSWORD").Value = "yyyyy"
End With
Set objCollection = IE.Document.getElementById("loginbutton")
objCollection.Click
'Second webpage
Do While IE.Busy Or IE.ReadyState <> 4: Loop ' opening the second webpage
Set HTMLDoc2 = IE.Document
With HTMLDoc2
**HTMLDoc2.getElementById("DEPARTMENTID").selectedindex = 1 'Drop down menu
HTMLDoc2.getElementById("DEPARTMENTID").FireEvent ("onchange")**
End With
Set objCollection = IE.Document.getElementById("loginbutton")
objCollection.Click
End Sub
Q)What code changes do I do to select Dwell_DF option Value 1567?
The above code gives run time error '424' : Object required.
HTMLDoc2.getElementById("DEPARTMENTID").selectedindex = 1 'Drop down menu
HTMLDoc2.getElementById("DEPARTMENTID").FireEvent ("onchange")
The above line give the error.
In the first webpage I fill the login credentials then in the next page is that of the image pasted with this post. Here I want to change the value in the drop down menu.
Give this a try. The value "1567" corresponds with the InnerText "Dwell_DF".
With HTMLDoc2
.getElementById("DEPARTMENTID").Focus
.getElementById("DEPARTMENTID").Value = "1567" 'You can also loop to find the text of the Option
.getElementById("DEPARTMENTID").FireEvent ("onchange")
End With
It should be something like this.
Sub passValueToComboBox1()
Dim ie As Object
Dim oHTML_Element As IHTMLElement
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "http://your_URL_here.php"
While ie.Busy Or ie.readyState <> 4: DoEvents: Wend
Set oHTML_Element = ie.document.getElementsByName("selectedReportClass")(0)
If Not oHTML_Element Is Nothing Then oHTML_Element.Value = "FUBU7"
For Each oHTML_Element In ie.document.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
End Sub
Check out the link below for some other ideas of how to programatically interact with a web site.
http://vbadud.blogspot.com/2009/08/how-to-login-to-website-using-vba.html