VBA - Logging into Website - Login Button - vba

Good afternoon all,
Please forgive me, I am new to VBA and have been practicing within my workbooks but for my work would be highly useful to branch out to Webscraping.
The following is my code.
Sub pullsalesdatafromonline()
Dim IE As Object
Dim doc As HTMLDocument
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "https://www.peoplestuffuk.com/WFMMCDPRD/Login.jsp"
Do While IE.Busy Or IE.readyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop
Set doc = IE.document
doc.getElementById("txtUserID").Click
doc.getElementById("txtUserID").Value = Usernameinserted
doc.getElementById("txtPassword").Click
doc.getElementById("txtPassword").Value = PasswordInserted
doc.getElementById("Login").Click
IE.navigate "https://www.peoplestuffuk.com/WFMMCDPRD/LoginSubmit.jsp"
IE.navigate "https://www.peoplestuffuk.com/WFMMCDPRD/ModuleSelection.jsp"
IE.navigate "https://www.peoplestuffuk.com/WFMMCDPRD/dashboard/main/dashboardmain.jsp"
IE.navigate "https://www.peoplestuffuk.com/WFMMCDPRD/rrd/matrixReport.jsp?fromPM=N"
End Sub
Here is the element that I am trying to scrape from;
<INPUT onclick=javaScript:submitForm() class=button-t type=button value=Login>
<SPAN id=Login_span class=buttonbox></SPAN>
I've looked at several other links that have looked into firing the .onclick event but, can't quite seem to figure it out.
The error in VBA highlights the:
IE.navigate "https://www.peoplestuffuk.com/WFMMCDPRD/ModuleSelection.jsp"
line. When my username and password enter, the page refreshes/ tries to exectute and then empties the Login and password info and leaves me on the Login page.
Would greatly appreciate someone's input.
Thank you

If you had a demo username and password to share then there was an efficient way of logging in using xhr. However, as for your current attempt concern, they are not the ids you used them within .getElementById(). Given that the following approach should log you in. Make sure to uncomment the delay, if the script fails to do the doing.
Sub pullsalesdatafromonline()
Dim IE As Object, doc As HTMLDocument
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "https://www.peoplestuffuk.com/WFMMCDPRD/Login.jsp"
While IE.Busy Or IE.readyState < 4: DoEvents: Wend
Application.Wait Now + TimeValue("00:00:03")
Set doc = IE.document
doc.querySelector("[name='txtUserID']").innerText = "username"
doc.querySelector("[name='txtPassword']").innerText = "password"
' Application.Wait Now + TimeValue("00:00:03")
doc.querySelector("input[value='Login']").Click
End Sub

Related

How do I get around breaking InternetExplorer Object with Application.SendKeys

I thought I was well on my way to success. What I'm attempting to do is Load a URL in IE, input some data, then run a report.
To accomplish this, I've created an InternetExplorer object, used that object to turn on visibility, and navigate to my URL.
Upon getting to the page, I have to supply some data to let the report generator do what it needs to do, then press enter. Once I'm to this point I need to do some navigation to different URLs until I arrive at the report page. To accomplish this, I'm attempting to return to using my InternetExplore object (IE) to navigate, but any time I use IE.anything the 462 error pops.
My question is two fold:
The object was good to begin with, I have to assume it was the use of Application.Send or Application.Wait that has somehow broke my object, why?
What did I do wrong?
Suggestions on how to resolve this?
Dim text As String
Dim IE
Set IE = CreateObject("InternetExplorer.Application")
'
IE.Visible = True
IE.Navigate "http://~~~~~~~~~.net/reports/views/result/reportResult.faces"
'
text = "somedata"
.
Application.Wait (Now + TimeValue("00:00:02"))
Application.SendKeys (text), True
'
Application.Wait (Now + TimeValue("00:00:02"))
Application.SendKeys vbCrLf, True
'
Application.Wait (Now + TimeValue("00:00:02"))
IE.Navigate "http://~~~~~~~~~.net/reports/views/myMrs/myFavorites.faces"
The end product was completely different than where I was headed. Thanks to those who provided input, especially Sorceri who turned me on to focusing on the elements.
Below is the finished code segment (I have more things I do after this but this gets me where I needed to go).
Private Sub OpenReport()
Dim text As String
Dim x As Integer
Dim IE
Set IE = New InternetExplorerMedium
text = "somecode"
'Load link
IE.Visible = True
IE.Navigate "http://~~~~~~~~~/reports/views/result/reportResult.faces"
'Wait for page to load
Do While IE.Busy Or IE.ReadyState <> 4
DoEvents
Loop
'fill in the pcode
IE.Document.getelementbyid("code").Value = text
'Click Submit
IE.Document.getElementsByClassName("btn btn-lg btn-primary btn-block")(0).Click
'Wait for page to load
Do While IE.Busy Or IE.ReadyState <> 4
DoEvents
Loop
'Click on Favorites
IE.Document.getElementsByClassName("myMRSFavoritesLink")(0).Click
'Wait for page to load
Do While IE.Busy Or IE.ReadyState <> 4
DoEvents
Loop
'Click on report
IE.Document.getElementsByName("myFavoritesForm:treeTable_tableId:1:infoButton101644806j_id__v_19")(0).Click
'Wait for page to load
Do While IE.Busy Or IE.ReadyState <> 4
DoEvents
Loop
Click on execute report
IE.Document.getElementsByName("setParametersForm:startReportButton")(0).Click
End Sub

Set doc = IE.document returns Automation Error

I'm trying to populate this form (https://help.grab.com/merchant/vi-vn/360027910731-DJang-ky-tham-gia-chuong-trinh-khuyen-mai-cung-GrabFood) with a set of excel data, filling each field of the form with respective column in Excel. I came across several tutorials on StackOverflow and Youtube on using VBA to open IE, navigate and fill in text box, from which I wrote these codes.
The problem comes when I run this code and Excel returns an error message box saying
Run-time Error '-2147467259 (80004005) Automation Error Unspecified Error.
I am too amateur to diagnose these codes that run perfectly on various videos on Youtube. Hope you guys can help me :)
Sub IE()
Dim IE As InternetExplorerMedium
Set IE = New InternetExplorerMedium
Dim doc As HTMLDocument
Set doc = IE.document //The error I mentioned above is in this line
IE.Visible = True
IE.navigate "https://help.grab.com/merchant/vi-vn/360027910731-DJang-ky-tham-gia-chuong-trinh-khuyen-mai-cung-GrabFood"
Do While IE.Busy
DoEvents
Loop
For Each ele In IE.document.getElementsByClassName("ant-radio-input")
If ele.Value = ThisWorkbook.Sheets("Sheet1").Range("A1") Then ele.Click: Exit For
Next
doc.getElementById("phone").Value = ThisWorkbook.Sheets("Sheet1").Range("C2").Value
doc.getElementById("email").Value = ThisWorkbook.Sheets("Sheet1").Range("D2").Value
IE.Quit
End Sub
You can't set the ie.document until after you have created the IE instance and the document is loaded.
IE.Visible = True
IE.navigate "https://help.grab.com/merchant/vi-vn/360027910731-DJang-ky-tham-gia-chuong-trinh-khuyen-mai-cung-GrabFood"
While IE.Busy Or IE.ReadyState <> 4:DoEvents:Wend
Set doc = IE.document

Clicking link within a HTML List

I'm trying use Excel's IE automation tools to click a website link within a users profile of this site. Using the following in VBA:
ie.Document.getElementsByClassName("website")(0).getElementsByTagName("a")(0).Click
But keep getting the Runtime error '438' when ran. Any advice on how to fix this or if clicking this link is even possible please? Thanks.
EDIT (Full Code):
Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "site"
Do While ie.READYSTATE <> READYSTATE_COMPLETE
DoEvents
Loop
Set objA = ie.Document.getElementsByClassName("website")(0) 'GETTIN ERROR HERE
objA.getElementsByTagName("a")(0).Click
End Sub
It would appear that there were a few minor issues here.
One being that .Click doesn't always function properly, I would suggest using .getAttribute("href") combined with ie.Navigate()
Another being that you seem to define html then never use it again.
The following code works:
Sub test()
Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "http://beverlyhills.yourkwoffice.com/mcj/user/AssociateSearchSubmitAction.do?orgId=5058&lastName=&firstName=&rows=100"
Do While ie.READYSTATE <> READYSTATE_COMPLETE
DoEvents
Loop
Set html = ie.Document
Set objA = html.getElementsByClassName("website")(0).getElementsByTagName("a")(0)
ie.Navigate (objA.getAttribute("href"))
End Sub

VBA - how to use google form on IE.navigate with process in the background

I'm currently using the following code to open an IE instance and navigate a complete-with-answers google form link. By accessing this link, the google form is immediately answered by the "entry.1102259784=XXXX" strings.
Private Sub Workbook_Open()
Application.ScreenUpdating = False
Dim IE As Object
Call Usernames
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
IE.Silent = True
IE.Navigate Cells(7, 1)
While IE.Busy
DoEvents
Wend
IE.Quit
Set IE = Nothing
End Sub
Its quite simple and works well (hidden and fast) except for this particular instance (google forms), as the IE opens and just stands there with the "Your form has been submitted" webpage on display.
The idea is to submit information about who is opening the file and when to a google spreadsheet.
I would appreciate any help on this matter.
Thank you very much!

IE page doesn't update - VBA

As far as I can see, there's nothing wrong with this code. Indeed, on one of my computers it works just fine, but on two others, it doesn't! All three run Excel 2003 on different versions of windows (7, 8.1 and 10).
What it is supposed to do is activate a search, then load the results page and print the innerHTML of the results page. One one machine (win 8.1) it does just that. On the other two, it prints the innerHTML of the search page.
Do I just have to abandon my trusty Excel 2003? Or is it a problem with my windows install? The W10 machine is a clean install of W10.
I am at my wits end over this! so please help me. :)
'This uses early binding
'In menu at top of page - Tools...Reference.... Microsoft Internet Controls and Microsoft HTML Object Library must both be ticked
Sub main()
sUrl = "https://www.insolvencydirect.bis.gov.uk/FIP1/"
Dim IE As InternetExplorer
Dim doc, element
Set IE = New InternetExplorer
IE.Visible = True
For iii = Asc("A") To Asc("B") ' *** test use B *** Do A-Z
aaa = Chr(iii)
IE.navigate sUrl
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Set doc = IE.document
doc.getElementById("IPSurname").Value = aaa
doc.forms.Item(0).elements(7).Click
'loading new page with data
Do While IE.Busy Or IE.readyState <> 4
DoEvents
Loop
Set doc = IE.document
MyBit = doc.body.innerHTML
Debug.Print MyBit
Next
End Sub
choose some element tag from the page and do that :
.
.
IE.navigate sUrl
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Set doc = IE.document
Do While True
If ie.document.getElementsByTagName("element tag").Length > 0 Then
Exit Do
End If
Application.Wait DateAdd("s", 1, Now)
Loop
.
.
.