VB click button on Website - vb.net

I'm using the following code to navigate to a web site and then predetermine the drop downs on the page and it works fine...
Private Sub CommandButton4_Click()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "https://postimage.io/"
IE.Visible = True
While IE.busy
DoEvents
Wend
IE.document.All("optsize").Value = "8"
IE.document.All("expire").Value = "0"
End Sub
... however there is a button on the page Choose images i would like my script to click that and in the pop-up enter a address of an image then press open
is this possible? if so could someone please help me achieve it

Related

How to click button when navigating website?

I'm trying to open a website, login, select report, and save in Excel format.
I managed to open IE, navigate to the site, enter Username/Password and login.
How do I select the webpage button to open the report page?
Below is the portion of the button on the website I'm attempting to click and code I've tried, both do not give any error message they simply don't do anything.
Website HTML
Sub vbamacro()
Dim IE As Object
Dim IEPage As Object
Dim IEPageElement As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
'Enter website name below
IE.Navigate "websitename"
'Ready state means wait until the website has loaded
Do Until IE.ReadyState = 4
DoEvents
Loop
Set IEPage = IE.Document
Set IEPageElement = IEPage.getElementById("ctl00__contentPlaceHolder__txtUserID")
IEPageElement.Value = "username"
Set IEPageElement = IEPage.getElementById("ctl00__contentPlaceHolder__txtPassword")
IEPageElement.Value = "password"
Set IEPageElement = IEPage.getElementById("ctl00__contentPlaceHolder__btnLogin")
IEPageElement.Click
Do Until IE.ReadyState = 4
DoEvents
Loop
With IE.Document
Set elems = .getElementsByTagName("Li")
For Each e In elems
If (e.getAttribute("ID") = "Reports3") Then
e.Click
Exit For
End If
Next e
End With
End Sub
Also tried;
For each e in ie.document.getElementsByTagName("Li")
If e.ID = "Reports3" Then
e.Click
Exit For
End If
Next
Try navigating directly to the href of the desired element
IE.navigate(e.href)
Also you are clicking “Li” tag element, you have to get the Li children’s tag witch is “a” (the one that has the href property)

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!

Need to use VBA to select an option from a drop down list in Internet Explorer

I have successfully got my code to open IE, navigate to the webpage I need, and login. I now need to select an option from a drop down list - please see the following html code:
html code for the list
How do I select the "TPS Managed Conservative - Dec 11" option from the dropdown.
My code so far:
Sub Strategic_Alpha_Monthly_Pivots_1_MASTER()
' open IE, navigate to the desired page and loop until fully loaded
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
my_url = "http://analytics.financialexpress.net/login.aspx"
With ie
.Visible = True
.navigate my_url
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
End With
' Input the userid and password
ie.document.getElementById("txtPassword").Value = "xxxxx"
' Click the "Search" button
ie.document.getElementById("btnAction").Click
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
ie.document.getElementById("ListPortfolio").Select
End Sub
selectedIndex or Value could be used. According to the screenshot the value 983678630 can be used. HTH
If Not VBA.IsNull(ie.document.getElementById("ListPortfolio")) Then
Dim htmlSelect
Set htmlSelect = ie.document.getElementById("ListPortfolio")
' htmlSelect.selectedIndex = 6
htmlSelect.Value = 983678630
Else
MsgBox "Element 'ListPortfolio' was not found", vbExclamation
End If

IE11 Automation: Change value of drop down list

I have included the code I use to navigate to a particular website; the end game here is to try and click the drop down list under "Login to" and select an option and click go. This is a learning experience for me. I would love to know the WHY? behind the code.
Sub internetautomation2()
Dim IE As InternetExplorer 'Created Class of type InternetExplorer
Set IE = New InternetExplorer 'Initiated Class, object IE created
'Properties and methods of
'Internet Explorer now usable
IE.Top = 0 'IE window properties Topleft corner
IE.width = 1500 '1500pixels wide
IE.Visible = True
IE.Navigate ("ibc.com") 'Navigate to particular website
While IE.ReadyState = READYSTATE_LOADING 'Wait for website to load
DoEvents
Wend
'Clicking drop down list,selecting option,click go
End Sub
Not sure what this question has to do with JavaScript, but just wire up a change event handler to your list and navigate to the list value:
Private Sub ComboBox1_Change()
IE.Navigate (ComboBox1.Value)
End Sub
This should do it:
'Set to the secod option "Deposit express"
IE.Document.getElementById("ctl00_IBCOtherOnlineServices_ddlLoginTo").selectedIndex = 1
'click the submit button
IE.Document.getElementById("ctl00_IBCOtherOnlineServices_btnLoginTo").Click

VBA to open URL, wait for 5 seconds, then open another URL

I have a webpage I want to open (URL is generated out of cell values in Excel), but going to that URL directly requires logging in to open. But if I first open the mainpage of the server, I have automatic login, and then I'm able to open the first URL without the need for user/pass.
So far I have this code, which opens IE with both URLs at the same time, in the same window, but different tabs, exactly as I want it to do, except URL2 requires the login.
To get around the login, I would like to add a pause between the navigate and navigate2, so the first page can complete loading before the second URL opens.
Can anyone help me with this?
Edit:
I have tried the suggestions from below, but it still needs the login. I have tried another solution, which is not optional, but it works. It consists of two buttons, running different macros, where the first one opens the main page to get the login, and the second one opens the next URL.
I have written them as follows:
First one:
Sub login()
Dim IE As Object
Const navOpenInNewTab = &H800
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://www.example.mainpage.com"
End Sub
Second one:
Sub search()
Dim IE As Object
Const navOpenInNewTab = &H800
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate2 "http://www.example.mainpage.com" & Range("w1").Cells.Value & Range("W2").Cells.Value, CLng(navOpenInNewTab)
End Sub
Is it possible to have a third macro running the other two with a delay between them?
Original code:
Sub open_url()
Dim IE As Object
Const navOpenInNewTab = &H800
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://www.example.mainpage.com"
'here I would like to add a pause for 5 seconds
IE.Navigate2 "http://www.example.mainpage.com" & Range("w1").Cells.Value & Range("W2").Cells.Value, CLng(navOpenInNewTab)
End Sub
Maybe it would be better to wait until the first page is fully loaded:
IE.Navigate "http://www.example.mainpage.com"
Do While IE.Busy Or Not IE.readyState = IE_READYSTATE.complete: DoEvents: Loop
IE.Navigate2 "http://www.example.mainpage.com" & Range("w1").Cells.Value & Range("W2").Cells.Value, CLng(navOpenInNewTab)
Note that the ReadyState enum READYSTATE_COMPLETE has a numerical value of 4. This is what you should use in the case of late binding (always the case in VBScript).
Do you mean something like:
Application.Wait(Now + TimeValue("00:00:05"))