VBA: Wait until browser loads more than once - vba

I've been working on a program to fill up a form after the webpage is loaded, its working well with this:
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
After this my code loads, fills up the form and the click the log in button.
Here comes the problem, i've been trying to click a button right after the new page loads, so i tried the same loop method to accomplish this, but it is not working.
I said to myself, this can't be, it has to work!, so i tried making a code for a search on youtube and for my surprise it did work, it searched a video and then i could select the one i wanted.
So if it does work, why it doesnt with my login page?
I noticed the page does not reach the next page right away after logging in, it loads a page and then quickly it loads another one and reroutes to that one.
Do you have any idea how to wait until the browser reaches that point?
whole code until now:
Private Sub CommandButton3_Click()
Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
On Error GoTo Err_Clear
MyURL = "url"
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.navigate MyURL
MyBrowser.Visible = True
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.document
HTMLDoc.all.user.Value = "user"
HTMLDoc.all.pass.Value = "pass"
For Each MyHTML_Element In HTMLDoc.getElementsByTagName("img")
If MyHTML_Element.getAttribute("src") = "url" Then MyHTML_Element.Click: Exit For
Next
Set MyBrowser.document = HTMLDoc
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.document
For Each MyHTML_Element In HTMLDoc.getElementsByTagName("img")
If MyHTML_Element.getAttribute("name") = "name" Then MyHTML_Element.Click: Exit For
Next
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub

Related

How to execute (Login) button on specific website?

I found this VBA code online and it works on Facebook for example.
On https://www.solarmanpv.com/portal/LoginPage.aspx it does not work. It opens Internet Explorer, puts the credentials on the right places but won't press login.
Error '424' is shown on VBA when I try to run.
Sub LoginViaBrowser()
Dim Dc_Usuario As String
Dim Dc_Senha As String
Dim Dc_URL As String
Dim objIE As New InternetExplorer 'Referencie "Microsoft Internet Controls"
objIE.Visible = True
Dc_Usuario = "user#email.com"
Dc_Senha = "pass"
Dc_URL = "https://www.solarmanpv.com/portal/LoginPage.aspx"
objIE.Navigate2 Dc_URL
Do Until objIE.readyState = READYSTATE_COMPLETE
DoEvents
Loop
objIE.document.all("uNam").innertext = Dc_Usuario
objIE.document.all("uPwd").innertext = Dc_Senha
objIE.document.all("login").submit
End Sub
Please try to use F12 developer tools to check the html elements, then, you could find that the id of the login button is "Loginning", not "login".
Try to modify your code as below:
Sub LoginViaBrowser()
Dim IE As Object
Dim Dc_Usuario As String
Dim Dc_Senha As String
Dim Dc_URL As String
Dim txtNam As Object, txtPwd As Object
Dc_Usuario = "user#email.com"
Dc_Senha = "pass"
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.Navigate "https://www.solarmanpv.com/portal/LoginPage.aspx"
While IE.ReadyState <> 4
DoEvents
Wend
IE.Document.getElementById("uNam").Value = Dc_Usuario
IE.Document.getElementById("uPwd").Value = Dc_Senha
IE.Document.getElementById("Loginning").Click
End With
Set IE = Nothing
End Sub
Have you tried calling a click on it by id?
IE.Document.getElementById("Loginning").Click

.Click action not doing anything - IE Simulation

I'm trying to simulate the interaction with Google through the IE app and going through the DOM to get the classes I need and all is fine, stepping though the code, except the .Click action which doesn't cause a crash but it doesn't do anything (page doesn't navigate) - Code and screenshot of HTML below:
Option Explicit
Private Sub Test_Automation()
Dim ie, doc, eInput, eButton, eButtons As Object
Dim sURL, sTest As String
Set ie = CreateObject("internetexplorer.application")
sURL = "https://www.google.co.uk/?gfe_rd=cr&ei=IpDvWK72LsjCaJCbjKAL&gws_rd=ssl"
sTest = "Test"
With ie
.Visible = True
.Navigate sURL
End With
Do While ie.Busy Or ie.readyState <> 4
DoEvents
Loop
Set doc = ie.document
Set eInput = doc.getElementByid("lst-ib")
Set eButtons = doc.getElementsByTagName("input")
eInput.Value = sTest
For Each eButton In eButtons
If (eButton.getattribute("name") = "btnK") Then
eButton.Click
Exit For
End If
Next
End Sub
Any advise on what I'm doing wrong would be great!
You can get rid of your For...Next loop at the bottom and replace it with this to click the button:
doc.forms(0).submit
The 0 can be changed to another number (such as 1 or 2) to click on a different button. If there are multiple buttons on a page that can be clicked on it will just take some trial and error to find out which number matches the button you want to click.

Issue with finding element on Amazon Seller homepage with VBA code

I recently came across a new problem while working with my Excel VBA code in order to automate the input a value from a cell (order number) into Amazon's search box and searching for the order. I can't seem to locate the proper object to reference the search button on the homepage of the Amazon Seller home page in order to click and proceed. My code is this:
Option Explicit
Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub MyAmazonSeller()
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
Dim oSignInLink As HTMLLinkElement
Dim oInputEmail As HTMLInputElement
Dim oInputPassword As HTMLInputElement
Dim oInputSigninButton As HTMLInputButtonElement
Dim oInputSearchOrder As HTMLInputElement
Dim oInputSearchButton As HTMLInputButtonElement
MyURL = "https://sellercentral.amazon.com/gp/homepage.html"
Set MyBrowser = New InternetExplorer
' Open the browser and navigate.
With MyBrowser
.Silent = True
.Navigate MyURL
.Visible = True
Do
DoEvents
Loop Until .ReadyState = READYSTATE_COMPLETE
End With
' Get the html document.
Set HTMLDoc = MyBrowser.Document
' See if you have the sign in link is because you are in the main
' page
Set oSignInLink = HTMLDoc.getElementById("signin-button-container")
If Not oSignInLink Is Nothing Then
oSignInLink.Click
Do
DoEvents
Loop Until MyBrowser.ReadyState = READYSTATE_COMPLETE
End If
' Get the email field and the next button
Set oInputEmail = HTMLDoc.getElementById("username")
Set oInputPassword = HTMLDoc.getElementById("password")
' Click the button and wait
oInputEmail.Value = "xxxxxxxxx#xxxxxx.net"
' Get the password field and the sign in button
Set oInputPassword = HTMLDoc.getElementById("password")
Set oInputSigninButton = HTMLDoc.getElementById("sign-in-button")
' Click the button and wait
oInputPassword.Value = "xxxxxxxx"
oInputSigninButton.Click
Do
DoEvents
Loop Until MyBrowser.ReadyState = READYSTATE_COMPLETE
Set oInputSearchOrder = HTMLDoc.getElementById("sc-search-field")
oInputSearchOrder.Value = "110-7706193-5695453"
Set oInputSearchButton = HTMLDoc.getElementByClassName("sc-search-button")
oInputSearchButton.Click
Do
DoEvents
Loop Until MyBrowser.ReadyState = READYSTATE_COMPLETE
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
The section right before the Err_Clear is the new snippet of code I've been trying to modify and work with. It seems that the search button does not have a proper ID, so therefore is more difficult to reference. I am getting an Error 91 every time the code gets to the second to last snippet.

VBA - Addressing Internet Explorer tabs

Strangely enough I didn't find any information on the topic and I'm currently stuck at the point where I managed to open a new tab in an instance of IE by programmatically clicking a button, but I haven't the faintest clue of how to address the new tab in order to get information from there (the button basically brings up a new tab with the result of a search).
This is basically a straightforward question, but I'm including my code anyway:
Sub AddInfoFromIntranet()
Dim Ie As SHDocVw.InternetExplorer
Dim URL As String
Dim iFrames As MSHTML.IHTMLElementCollection
Dim iFrame As MSHTML.HTMLFrameElement
Dim Doc As MSHTML.HTMLDocument
Dim InputBox As MSHTML.IHTMLElementCollection, htmlButton, allTags, Tag
' Opens Intranet - yeah, sadly it's not a public web page
URL = "{My intranet website}"
Set Ie = New SHDocVw.InternetExplorer
With Ie
.navigate URL
.Visible = True
While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
Set Doc = .document
End With
' Gets top_window frame and navigates to it, then inserts the name to search
Set iFrames = Doc.getElementsByName("top_window")
If Not iFrames Is Nothing Then
Set iFrame = iFrames(0)
Ie.navigate URL & iFrame.src
While Ie.Busy Or Ie.readyState <> READYSTATE_COMPLETE: DoEvents: Wend
Set InputBox = Doc.getElementsByName("Nachnamevalue")
If Not InputBox Is Nothing Then InputBox(0).Value = "test"
' Clicks on "search"
Set allTags = Doc.getElementsByTagName("input")
For Each Tag In allTags
If Tag.Value = "suchen" Then
Tag.Click
Exit For
End If
Next
' Here a new tab is opened, must find info in this tab
While Ie.Busy Or Ie.readyState <> READYSTATE_COMPLETE: DoEvents: Wend
' HERE I HAVE NO CLUE WHAT TO WRITE. THE CODE ABOVE WORKS FLAWLESSLY
End If
Set Doc = Nothing
Set iFrames = Nothing
Set iFrame = Nothing
Set InputBox = Nothing
Set allTags = Nothing
Set Ie = Nothing
Ie.Quit
End Sub
Now, is there a way to address a tab by: 1) its name (and where do I find it) 2) its position in browser 3) the status (if it is "active") ?
Bonus questions: since I am new to VBA and Internet Explorer interaction, what exactly are the variables: htmlButton, allTags, Tag ? Also, could anyone explain if I need to set all the variables at the end to nothing, or I just need to set the Internet Explorer to nothing?
Thanks in advance!
See below for a function you can use to get an open IE document window - I don't think IE exposes any simple (VBA-accessible) API for working directly with tabs or determining whether a specific tab is active.
allTags is a collection of DOM elements with type "" , and Tag is a single memeber of that collection.
You do not have to set objects to Nothing before exiting a Sub (though some people still do that) - the VBA runtime will take care of that for you.
Sub TestGetIE()
Dim IE As Object
Set IE = GetIE("http://stackoverflow.com")
If Not IE Is Nothing Then
IE.document.execCommand "Print", False, 0
End If
End Sub
'Get a reference to an open IE window based on its URL
Function GetIE(sLocation As String) As Object
Dim objShell As Object, objShellWindows As Object, o As Object
Dim sURL As String
Dim retVal As Object
Set retVal = Nothing
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows
For Each o In objShellWindows
sURL = ""
On Error Resume Next
'check the URL and if it's the one you want then
' assign it to the return value
sURL = o.document.Location
On Error GoTo 0
'Debug.Print sURL
If sURL Like sLocation & "*" Then
Set retVal = o
Exit For
End If
Next o
Set GetIE = retVal
End Function

VBA access remote website, automate clicking submit after already automating clicking button on other webpage

I'm working with vba in excel 2010 and internet explorer 8 and Vista. The code below works to go to a remote website and post a form. On the resulting page, the code should click the "get estimates" button. Instead I get this error "object variable or with block variable not set". The highlighted problem line in the code is "estimate = ieApp.document.getElementById("btnRequestEstimates")".
I think part of the problem might be that the button that isn't working is a submit button that isn't part of a form. I am also wondering if the variables need to be reset before the 2nd button click. The error message implies this is a qualification problem, but I think it's a pretty standard way of qualifying an element in this situation. Those are some things I've been googling to no avail, I'm not really sure what the problem is.
Sub btn_version()
Dim ieApp As Object
Dim ieDoc As Object
Dim ieForm As Object
Dim ieObj As Object
Dim URL As String
Dim estimate As Object
URL = "http://www.craft-e-corner.com/p-2688-new-testament-cricut-cartridge.aspx"
Set ieApp = CreateObject("InternetExplorer.Application")
ieApp.Visible = True
ieApp.navigate URL
While ieApp.Busy Or ieApp.readyState <> 4: DoEvents: Wend
Set ieDoc = ieApp.document
Set ieForm = ieDoc.forms(1)
For Each ieObj In ieForm.Elements
If ieObj.ClassName = "AddToCartButton" Then
ieObj.Click
End If
Next ieObj
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
While ieApp.Busy Or ieApp.readyState <> 4: DoEvents: Wend
estimate = ieApp.document.getElementById("btnRequestEstimates")
estimate.submit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End Sub
The code below combines your xmlhttp code from automate submitting a post form that is on a website with vba and xmlhttp (to give you better control on the POST, ie skipping your Set ieDoc = ieApp.document section in our question) with clicking the "btnRequestEstimates button on the final URl from this page
Sub Scrape2()
Dim objIE As Object
Dim xmlhttp As Object
Dim ieButton As Object
Dim strResponse As String
Dim strUrl As String
strUrl = "http://www.craft-e-corner.com/addtocart.aspx?returnurl=showproduct.aspx%3fProductID%3d2688%26SEName%3dnew-testament-cricut-cartridge"
Set objIE = CreateObject("InternetExplorer.Application")
objIE.navigate "about:blank"
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
'~~> Indicates that page that will receive the request and the type of request being submitted
xmlhttp.Open "POST", "http://www.craft-e-corner.com/addtocart.aspx?returnurl=showproduct.aspx%3fProductID%3d2688%26SEName%3dnew-testament-cricut-cartridge", False
'~~> Indicate that the body of the request contains form data
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
'~~> Send the data as name/value pairs
xmlhttp.Send "Quantity=1&VariantID=2705&ProductID=2688"
strResponse = xmlhttp.responseText
objIE.navigate strUrl
objIE.Visible = True
Do While objIE.readystate <> 4
DoEvents
Loop
objIE.document.Write strResponse
Set xmlhttp = Nothing
Set ieButton = objIE.document.getelementbyid("btnRequestEstimates")
ieButton.Click
End Sub