I want to click the button in VBA - html

I am a beginner and non-technical person, I am completely stuck here
I want to click the New Project button using VBA
Here is the inspect Element for New project button
<button tabindex="0" class="xui-button xui-button-create xui-button-small" type="button" data-automationid="project-modal-new-button">New project</button>
Below is the code, I have created so far, it's running without any issues and it will take me to the project tab from the Project Tab I need to click the new Project Button using VBA
Sub Xero()
Dim i As SHDocVw.InternetExplorer
Set i = New InternetExplorer
i.Visible = True
i.navigate ("https://login.xero.com/")
Do While i.readyState <> READYSTATE_COMPLETE
Loop
Dim idoc As MSHTML.HTMLDocument
Set idoc = i.document
idoc.all.UserName.Value = "XXXXX"
idoc.all.Password.Value = "123"
Dim ele As MSHTML.IHTMLElement
Dim eles As MSHTML.IHTMLElementCollection
Set eles = idoc.getElementsByTagName("button")
For Each ele In eles
If ele.ID = "submitButton" Then
ele.Click
Application.Wait (Now + TimeValue("0:00:10"))
Else
End If
Next ele
i.navigate ("https://projects.xero.com/?CID=!QhJgj")
Do While i.readyState <> READYSTATE_COMPLETE
Loop
End Sub
I want to click the New project button to move further

Try using a css class selector (same idea as getElementsByClassName but faster)
While i.Busy Or i.readyState < 4: DoEvents: Wend
Application.Wait Now + Timeserial(0,0,2)
i.document.querySelector(".xui-button-create").click
Also, use proper page waits after .click and .navigate i.e.
While i.Busy Or i.readyState < 4: DoEvents: Wend
So, have that proper wait between .navigate and the attempt to click the button and again after clicking the button.

Related

Loop through dropdown browsermenu with VBA

I need to loop through a dropdown list of a specific website and download the latest file for each entry. I managed to open the website and click the "submit" button, but I can´t find a solution to loop through all the dropdown entries.
Dim reportnr As String
Dim internetadress As String
Dim btn As Object
Dim IE As SHDocVw.InternetExplorer
reportnr = 10
internetadress = adress & reportnr
Set IE = CreateObject('InternetExplorer.Application')
IE.Visible = True
IE.navigate internetadress
Do While IE.Busy
Application.Wait DateAdd("s", 2, Now)
Loop
While IE.Busy
DoEvents
Wend
For Each btn In IE.document.getElementsByClassName("btn btn-primary")
If btn.name = "submit" Then
btn.Click
End If
Next btn
The code on the website regarding the dropdown below.
I tried several approchoaches, but I´ll get error or just nothing happens. Last thing I tried is this :
Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub SelectQuantity()
Dim ie As New InternetExplorer, numberOfOptions As Long, i As Long
With ie
.Visible = True
.Navigate2 "https://www.amazon.com/belif-True-Cream-Aqua-Korean/dp/B00H4GOAZO/ref=pd_cart_crc_cko_cp_2_2/139-8277217-3794320?_encoding=UTF8&pd_rd_i=B00H4GOAZO&pd_rd_r=e154e278-8a11-4ab0-8173-5d0dbaff1938&pd_rd_w=Hm8FW&pd_rd_wg=Hpv4X&pf_rd_p=eff166ab-25d2-4f2c-a51d-0a0e86061f9d&pf_rd_r=EVT26E6K7CV8T1QMTY7H&psc=1&refRID=EVT26E6K7CV8T1QMTY7H"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
numberOfOptions = .querySelectorAll("#quantity option").Length 'gather option tag element children of parent select element with id quantity
For i = 1 To numberOfOptions
.querySelector("#quantity [value='" & i & "']").Selected = True
ActiveSheet.Cells(i, 1) = i
Next
Stop
End With
End With
End Sub
Source: VBA loop through dropdown elements from web page and download to excel sheet
Thanks in advance for help.
From the website you provide, you can try the following code to loop through the dropdown list to choose the option you want:
Set lists = IE.document.getElementsByClassName("active-result") 'get all the options li elements
For Each liElement In lists
If liElement.innerText = "something" Then
liElement.setAttribute "class", "active-result result-selected"
End If
Next liElement
I find that the chosen option has different class which is active-result result-selected, so I think maybe you can change the class to select the option.

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

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

VBA to click a button on a website and extract tables

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.