VBA to click a button on a website and extract tables - html

I want to click a button on a website with VBA and extract the table which appears when clicked.
My code is here but it is not working right now, how can I do that?
I want to click the "Get Price Quoted" button on the right corner of the table and extract appearing information.
Sub Test()
Dim IE As Object
Dim doc As Object
Dim strURL As String
Dim objElement As Object
Dim objCollection As Object
strURL = "http://financials.morningstar.com/competitors/industry-peer.action?t=GOOG&region=usa&culture=en-US"
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.navigate strURL
Do Until .ReadyState = 4: DoEvents: Loop
Do While .Busy: DoEvents: Loop
Dim Item As Object
Set Item = IE.document.getElementsByClassName("hspacer0")
Item.Item(0).Click
Do Until .ReadyState = 4: DoEvents: Loop
Do While .Busy: DoEvents: Loop
Set doc = IE.document
GetAllTables6 doc
.Quit
End With
End Sub

You are not clicking the correct button the code. I am not sure what button if any you are clicking. But if you run the javascript code loadIndustryPeersQuoteData(1) it will 'click' the button and then you will need to code getting the table.
That 'hspacer0' is just a blank spacer that contains no objects or anything to click.

Related

Use VBA to find submenu element and click

I am fairly new to vba and am self taught but have gotten my vba to log in to my website and get to the homepage. Next I need to click on a submenu item but I am struggling to find it. When manually doing this, the submenu works when I hover over the icon and the click on a button called "Comed Reports" below which I believe is the element ID "Report1017".
Below is the html code from the website:
And below is where my code is at this stage:
Sub Login()
Const Url$ = "examplewebsite.com"
Dim HTMLDoc As HTMLDocument
Dim oHTML_Element As IHTMLElement
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Navigate Url
.Visible = True
Do While ie.Busy Or ie.ReadyState < 4
DoEvents
Loop
Set HTMLDoc = ie.Document
Dim Login As Object
Dim Password As Object
Dim LoginButton As Object
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Name Like "txt_1*" Then Set Login = oHTML_Element
If oHTML_Element.Name Like "txt_2*" Then Set Password = oHTML_Element
If oHTML_Element.Name Like "btnLogin*" Then Set LoginButton = oHTML_Element
Next
Login.Value = ""
Password.Value = ""
LoginButton.Click
Do While ie.Busy Or ie.ReadyState < 4
DoEvents
Loop
' Find Submenu Report1017 here
End With
End Sub
If it is always Report1017 you probably just need to run the javascript:
HTMLDoc.parentWindow.execScript "LoadContent('Report1017','ReportList.aspx?ReportParam=1017')", "JavaScript"
Give it a try.

How to click menubar option in a website?

I want to code to click a menu in a website.
Sub SearchBot()
Dim ie As Object
Dim HTMLDoc As MSHTML.HTMLDocument
Dim ckt_No As String
ckt_No = Range("A2").Value
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ShowWindow ie.hwnd, SW_MAXIMIZE
ie.Navigate "http://gamit.web.att.com/gamitx/welcome.htm"
Do While ie.Busy = True Or ie.ReadyState <> 4: DoEvents: Loop
Set HTMLDoc = ie.Document
HTMLDoc.getElementById("yui-gen1").Focus
HTMLDoc.getElementById("yui-gen1").Click
Do While ie.Busy = True Or ie.ReadyState <> 4: DoEvents: Loop
Stop 'Press SHIFT + F9 and examine the window...
The menu
while inspect the menu I got the HTML Code
VBA didn't throw any error, but it does not click the menu.
After running the code the menu looks like this
I have Microsoft Internet control, Microsoft HTML object library functions in my VBA editor.
Try navigating to the href of the child a tag (getting the parent by its id)
ie.navigate2 HTMLDoc.querySelector("#yui-gen1 a").href
Maybe this, but I can't test it as your url is not working for me
HTMLDoc.getElementById("yui-gen1").nextPageElement.Click

reading the elements of 2nd page after submit click

I am working on a web form fill using excel cells data, and the issue here is that after filling the text in the first page using the elements ID, when it click on Submit it loads a new IE.
Now here I am not able to fill the text using the elements ID etc, as its (VBA) not identifying it or not considering it.
IE Page loads properly here no issue.
Sub Test1()
Dim IE As Object
Dim doc As HTMLDocument
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "https://xyzfrmTarget=New&Text_ident=002719&frmOption=CREATE"
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Set doc = IE.document
'IE.document.getElementById().Value="test"
'doc.getElementById("ui-new_widget-1_new_company_12").Value = ThisWorkbook.Sheets("Data").Range("A2").Value
doc.getElementById("ui-new_widget-1_monitor_12").Value = ThisWorkbook.Sheets("Data").Range("A2").Value
doc.getElementById("new_date").Value = "2019-01-19"
'ThisWorkbook.Sheets("Data").Range("c2").Value
doc.getElementById("new_no_mail").Click
IE.document.forms(0).submit
End Sub
It submit the 1st page with the Text data from excel, but issue with the 2nd page which loads
Try using proper waits after navigate and .click and work off .document. This should update with new content after the .click
Option Explicit
Public Sub PerformActions()
Dim IE As InternetExplorer
Set IE = New InternetExplorer
With IE
.Visible = True
.Navigate2 "https://xyzfrmTarget=New&Text_ident=002719&frmOption=CREATE"
While .Busy Or .readyState < 4: DoEvents: Wend
.document.getElementById("ui-new_widget-1_monitor_12").Value = ThisWorkbook.Sheets("Data").Range("A2").Value
.document.getElementById("new_date").Value = "2019-01-19"
.document.getElementById("new_no_mail").Click
While .Busy Or .readyState < 4: DoEvents: Wend
'.document ''< do whatever references off .document here
'other code
'.quit
End With
End Sub

Why my google search macro is returning error 424 - Object required?

I have this simple macro that opens up IE and type a word in search box. It was working before, but now is returning
Error 424 - Object required.
Any ideas on what is happening? Is there any lib reference that I'm missing?
Sub AcessaPaginaDados()
Dim xobj As HTMLDivElement
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate ("https://www.google.com/")
Do While ie.Busy Or ie.readyState <> 4
DoEvents
Loop
Set xobj = ie.document.getElementById("lst-ib")
Call xobj.setAttribute("value", "teste")
End Sub
That id doesn't appear against the input box when I open google. You can use an attribute = value selector to target the title attribute of the search box. Right-click inspect the search box and see what your local language attribute value is for title=. In U.K. the value is Search. Use whatever your local value is.
Element html:
Other:
Also, .navigate2 is the preferred method.
If the page is slow loading you may need a loop until element present.
Option Explicit
Public Sub AcessaPaginaDados()
Dim xobj As HTMLDivElement
With CreateObject("InternetExplorer.Application")
.Visible = True
.Navigate2 "https://www.google.com/"
While .Busy Or .readyState < 4: DoEvents: Wend
Set xobj = .document.querySelector("[title=Search]")
xobj.Value = "teste"
Stop
.Quit
End With
End Sub

Excel VBA Select Option Website Dropdown List and Click

I am trying to copy the response from the website, but I have to select one of the options from the dropdown list so the page can refresh.
So far, I've managed to open IE and click the dropdown button, but I cannot select the option, don't know why. Please find the a sample of the code below.
Sub OpenWebSelectClickSaveHtmlFile()
Dim URL As String
URL = "http://formulaacademia.com.br/academia"
Set myIE = CreateObject("InternetExplorer.Application")
With myIE
.navigate URL
.Visible = True
Do While .Busy = True Or .readyState <> 4
Loop
DoEvents
Set selectitems = myIE.document.getElementsByTagName("button")
For Each i In selectitems
i.Click
Next i
End With
End Sub
Rgds
It should be something like this. Change the relevant parts to suit your needs.
Sub passValueToComboBox1()
Dim ie As Object
Dim oHTML_Element As IHTMLElement
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "http://peterschleif.bplaced.net/excel/combobox/index.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 = "com.db.moap.report.FUBU7"
For Each oHTML_Element In ie.document.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
End Sub