So I have some code to go to a website and then login. Then I need it to go to the password change URL. Sometimes when I run the code, I get the following error:
Runtime Error 424: Object Required
Sometimes the debugger says it is the first getelementbyid statement, but most times it says the issue is the last three getelementbyid statements. The code is below:
Dim ie As Variant
Dim strURL As String
Sub login()
Set ie = New SHDocVw.InternetExplorer
ie.Visible = True
ie.navigate "https://minecraft.net/profile"
While ie.Busy
DoEvents
Wend
ie.document.getElementById("username").Value = "ddd"
ie.document.getElementById("password").Value = "ddddddddddd"
Dim htmlForm As HTMLFormElement
Set htmlForm = ie.document.getElementById("loginForm")
htmlForm.submit
' **********************************************************************
'IE.Document.getElementById("username").Value = "ddddd"
' IE.Document.getElementById("password").Value = "ddddd"
' IE.Document.getElementById("signin").Click
'**********************************************************************
'Pause while page loads
Application.Wait (Now + #12:00:03 AM#)
ie.navigate "https://minecraft.net/profile/password"
ie.document.getElementById("oldPassword").Value = "oldpass"
ie.document.getElementById("password").Value = "enwapss"
ie.document.getElementById("passwordConfirmation").Value = "enwapss"
Set htmlForm = ie.document.getElementById("loginForm")
htmlForm.submit
End Sub
Thanks in advance!
It may be that the website isn't in a ready state, as in the site hasn't fully loaded when it's attempting to input the values.
After
ie.navigate "https://minecraft.net/profile/password"
Try adding
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
This will loop until the webpage has loaded similar, to the way you've done it in your above code with
Application.Wait (Now + #12:00:03 AM#)
Related
I am trying to capture data from Url with Query Selector getting some error . as I am not an expert on vba. please someone can help me solve this here I enclosed my code
Public Sub CommandButton1_Click()
'On Error Resume Next
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "https://g.page/shrungarbeauty?share" ' try with first URLS and second
Do
DoEvents
Loop Until ie.readystate <> 4
Application.Wait (Now + TimeValue("00:00:15"))
title As Object
Set title = ie.document.querySelector("section-hero-header-title-title GLOBAL__gm2-headline-5").innerText
ActiveCell = title
ie.Quit
Set ie = Nothing
End Sub
I am trying to learn web scraping with VBA and i am running into an issue in the most basic first step.
The problem (I think) is that after IEObject.Navigate is performed, all the variables of IEObject dissapear. So I get the error
"Run-time error '426': The remote server machine does not exist or is
unavailable"
on line 8. Any help is appreciated.
Code:
Sub VBAWeb()
Dim IEObject As InternetExplorer
Set IEObject = New InternetExplorer
IEObject.Visible = True
IEObject.Navigate URL:="https://google.com"
Do While IEObject.Busy = True Or IEObject.ReadyState <> READYSTATE_COMPLETE
Application.Wait Now + TimeValue("00:00:01")
Loop
Debug.Print IEObject.LocationURL
End Sub
I suggest you try to run code below on your side may help to fix the issue.
Sub demo()
Dim URL As String
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "https://www.microsoft.com"
IE.Navigate URL
Do While IE.ReadyState = 4: DoEvents: Loop
Do Until IE.ReadyState = 4: DoEvents: Loop
Debug.Print IE.LocationURL
Set IE = Nothing
End Sub
You can see that the IE object was created differently. Also, the loop is a little bit different.
I have a macro that connects to a web-page, gets some info and displays it on a user form (for further actions):
Private Sub UserForm_Initialize()
Dim login As String, password As String
Dim waittime As String
Dim arrForms As Variant
Dim ie As InternetExplorer
LoadingForm.Show vbModeless
LoadingForm.lblLoading.Caption = "Loading..."
waittime = "0:00:02"
login = "lgn"
password = "passwrd"
txtEndDate.Value = Format(Now(), "dd.mm.yyyy")
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
'open login page
ie.navigate "my link (internal company database)"
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
Application.Wait (Now + TimeValue(waittime))
'if already logged in, skip
On Error GoTo Skip_login
ie.document.getElementById("_58_login").Value = login
ie.document.getElementById("_58_password").Value = password
ie.document.getElementsByClassName("btn btn-primary")(0).Click
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
Application.Wait (Now + TimeValue(waittime))
Skip_login:
ie.navigate "my link (internal company database)"
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
Application.Wait (Now + TimeValue(waittime))
arrForms = GetListOfForms(ie) 'this is a function that reads the page,
'finds a table and converts its values to an array
'I tried to comment this line (maybe a bug in my function)
'but even without it the code does not work
For i = LBound(arrForms) To UBound(arrForms)
FormsList.AddItem (arrForms(i))
Next i
'Log out and quit IE
ie.navigate "my link (internal company database)"
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
Application.Wait (Now + TimeValue(waittime))
ie.document.getElementById("sign-out").Click
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
Application.Wait (Now + TimeValue(waittime))
ie.Quit
LoadingForm.Hide
End Sub
It worked fine for a few days. It also worked today. But it stopped working for an unknown reason (it just says"automation error" without reference to an error in my code). I was using it, closed the user form, then clicked to start macro again and it does not work. I tried to open older backups - they stopped working too. I did not change anything (links, login/password, any other line of code).
My user form does not have any uncommon user controls. It has the following controls only: text fields, lables, frames, listbox, and buttons)
I've been searching for an answer for days. Please help.
<div class="FBOSearchResultsItemPricingTablePrice">$3.8391</div>
I just need to pull the "$3.8391" from this string within the website and have it populate into a cell in Excel.
Here's what I have so far, all of which works exactly how I need it to. I have gotten the web page to open, log in and search for a location, however, I can't seem to get this one simple number!!
(I obviously changed the username/password in the code to protect my information)
Sub login()
Dim IE As Object
Dim doc As Object
Set IE = CreateObject("InternetExplorer.application")
IE.AddressBar = 0
IE.StatusBar = 0
IE.Toolbar = 0
IE.Visible = True
IE.navigate "https://www.avfuel.com/login"
Do Until Not IE.busy: DoEvents: Loop
Set doc = IE.document
Do While doc.ReadyState <> "complete": DoEvents: Loop
doc.getElementsByName("dnn$ctr2046$Login$Login_AvfuelAuth$txtUserName")(0).Value = "XXXX"
doc.getElementsByName("dnn$ctr2046$Login$Login_AvfuelAuth$txtPassword")(0).Value = "XXXXX"
doc.getElementById("dnn_ctr2046_Login_Login_AvfuelAuth_cmdLogin").Click
Application.Wait (Now + TimeValue("00:00:05"))
doc.getElementsByName("dnn$ctr618$Loader$ctl00$txtAirportSearchInput")(0).Value = "bfl"
doc.getElementById("dnn_ctr618_Loader_ctl00_hlSearchPageAirport").Click
Application.Wait (Now + TimeValue("00:00:05"))
End Sub
Try this
Cells(1,1) = doc.getElementsByClassName("FBOSearchResultsItemPricingTablePrice")(0).innerText
Or use CSS selector
e.g.
[A1] = doc.querySelector(".FBOSearchResultsItemPricingTablePrice").innerText
The "." is for class.
I have a short excel macro that is designed to:
1) Open Internet Explorer and navigate to "http://www.puco.ohio.gov/pucogis/address/search.cfm"
2) Fill out a form on that site with data from the excel workbook
3) Click a button to submit the form
4) Scrape some innertext from the website and place it in a cell in the workbook
5) Close Internet Explorer
I can not get step 3 to work. That is, I can not get the click/submit function to work with this website. When the button is clicked the website populates with information specific to the information entered in the form. Everything else in the code is working. I have searched for an answer and tried the submit verses click approach with no luck.
Thanks for you help.
Code below:
Private Sub SiteData()
Dim ie As Object
Dim utility As Variant
Dim HTMLButton
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://www.puco.ohio.gov/pucogis/address/search.cfm"
ie.Visible = True
While ie.Busy
DoEvents
Wend
ie.Document.all("address").Value = ThisWorkbook.Sheets("Site Info").Range("D14")
While ie.Busy
DoEvents
Wend
Set HTMLButton = ie.Document.getElementsByTagName("input")(1)
HTMLButton.Click
While ie.Busy
DoEvents
Wend
Set utility = ie.Document.getElementById("supName")
ThisWorkbook.Sheets("Site Info").Range("D50") = utility.innerText
ie.Quit
Set ie = Nothing
End Sub
Try this solution, which I found from this answer to a similar question. That answer was not accepted, but I have tested this with your code and seems to be working.
Private Sub SiteData()
Dim ie As Object
Dim utility As Variant
Dim HTMLButton
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://www.puco.ohio.gov/pucogis/address/search.cfm"
ie.Visible = True
While ie.Busy
DoEvents
Wend
ie.Document.all("address").Value = ThisWorkbook.Sheets("Site Info").Range("D14")
While ie.Busy
DoEvents
Wend
Call ie.Document.parentWindow.execScript("codeAddress()")
While ie.Busy
DoEvents
Wend
Set utility = ie.Document.getElementById("supName")
ThisWorkbook.Sheets("Site Info").Range("D50") = utility.innerText
ie.Quit
Set ie = Nothing
End Sub
If you don't know or can't reasonably anticipate the function call codeAddress(), then you can try something like this to derive it from the button's onclick property:
Dim fn$
fn = HTMLButton.onclick
fn = Mid(fn, InStr(fn, "{"))
fn = Trim(Replace(Replace(Replace(fn, "{", vbNullString), "}", vbNullString), vbLf, vbNullString))
Call ie.Document.parentWindow.execScript(fn)
You can call the JavaScript directly. try this it will work
Instead of:
Set HTMLButton = ie.Document.getElementsByTagName("input")(2)
HTMLButton.Click
use
ie.Document.parentWindow.execScript code:="codeAddress()"
note that IE may prompt you to confirm every run so you may need to
stop showing this message for smooth operation
Private Sub CommandButton1_Click()
Dim ie As Object
Dim utility As Variant
Dim HTMLButton
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://www.puco.ohio.gov/pucogis/address/search.cfm"
ie.Visible = True
While ie.Busy
DoEvents
Wend
ie.Document.all("address").Value = ThisWorkbook.Sheets("Site Info").Range("D14")
While ie.Busy
DoEvents
Wend
ie.Document.parentWindow.execScript code:="codeAddress()"
'Set HTMLButton = ie.Document.getElementsByTagName("input")(2)
'HTMLButton.Click
While ie.Busy
DoEvents
Wend
Set utility = ie.Document.getElementById("supName")
ThisWorkbook.Sheets("Site Info").Range("D16") = utility.innerText
ie.Quit
Set ie = Nothing
End Sub
thanks also to this article helped me to solve your problem
How to find and call javascript method from vba