VBA Select Item from List in Internet Explorer - javascript

I'm trying to create a macro that will perform certain tasks on a webpage. What I'm struggling with is to select an option from a dropdown list.
This is (part of) my VBA Code:
Sub stackoverflow()
' ***** Dims for VBA *****
Dim objIE As SHDocVw.InternetExplorer
Dim ieDoc As MSHTML.HTMLDocument
Dim testURL As String
' ***** END *****
' ***** Dim Buttons*****
Dim StatusFld As MSHTML.HTMLInputElement
' ***** END *****
testURL = "https://test.com"
'Create Internet Explorer Object
Set objIE = New SHDocVw.InternetExplorer
'Navigate to URL
objIE.navigate testURL
'Wait until page has loaded up
Do Until objIE.readyState = READYSTATE_COMPLETE: Loop
'Get document object
Set ieDoc = objIE.document
objIE.Visible = True
'***** Button IDs *****
Set statusFld = ieDoc.all.Item("apprverStatusCode")
'***** END *****
'THIS IS THE ROW WITH THE ERROR:
objIE.document.getElementById(statusFld).Value = "Rejected"
So with the above code, I get "Run time error '424' - Object required".
I've also tried this, which was also not successful:
statusFld.Value = "Rejected"
This is the source code of the website:
<select id="apprverStatusCode" data-code="GROUP_STATUS" data-servicecode="{"preview":"20501","download":"20502"}"
>
<option value="ALL">All</option>
<option value="Rejected">Rejected</option>
<option value="Approved">Approved</option>
</select>
Any ideas why this isn't working?

objIE.document.getElementById("apprverStatusCode").Value = "Rejected"
However that isn't how elements are selected. You need to iterate through the options and add a "selected" attribute to the one you want to select, and remove it from any you do not.
Dim oOptions As MSHTML.IHTMLElementCollection
Dim oOption As MSHTML.IHTMLOptionElement
Set oOptions = objIE.document.getElementById("apprverStatusCode").getElementsByTagName("option")
For Each oOption In oOptions
If oOption.Value = "Rejected" Then
oOption.Selected = True
Else
oOption.Selected = False
End If
Next oOption
I haven't tested this code, so not sure if the datatypes are correct/will work in the for each.
See https://stackoverflow.com/a/38658524/212869 it looks a bit cleaner than mine.

Related

Using a VBA script to login to a website Login Page

I am trying to use sources on google and on this site to help me but I am struggling to enter a username and password to a input box on a login page.
The page is on a local network on our work server.
The HTML code for the login page:
<TD background=/frontend/images/greenback.gif width=302><INPUT maxLength=32 name=username> </TD>
<TD background=/frontend/images/greenback.gif width=302><INPUT maxLength=32 type=password value="" name=password> </TD>
So far I have tried:
Sub Test()
On Error Resume Next
Dim sUrl As String: sUrl = "https://workplace.plus.net/index.html?"
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate sUrl
Do While ie.readystate <> 4: DoEvents: Loop
ie.document.getElement.Name("username").Value = 1234
End Sub
I have also tried:
ie.document.getElement.Name("username").Value = 1234
ie.document.getElement.Id("username").Value = 1234
ie.document.getElement.Class("username").Value = 1234
ie.document.getElement.Type("username").Value = 1234
ie.document.getElementByClassName("username").Value = 1234
ie.document.getElementById("username").Value = 1234
ie.document.getElementByName("username").Value = 1234
ie.document.getElementByType("username").Value = 1234
The thing is I used this same code on google with the ID button and it works. Is there something I am not taking into consideration?
Here i'm using the facebook login page. You can put any other link to login, put sUser as string and sPass as string. Assigned the values you want to input. Application.Wait (Now + TimeValue("00:00:02")) is the time between loading the page and add login id and password. On the other hand will pass the user id and password IE.Document.all.Item("email").Value = sUser. Try this code please
Sub facebookPost()
Dim IE As Object
Dim sUser As String, sPass As String
sUser = "1234"
sPass = "1234"
Set IE = CreateObject("INTERNETEXPLORER.Application")
IE.Navigate ("www.Facebook.com")
IE.Visible = True
Do While IE.Busy: DoEvents: Loop
Application.Wait (Now + TimeValue("00:00:02"))
IE.Document.all.Item("email").Value = sUser
IE.Document.all.Item("pass").Value = sPass
IE.Document.all.Item("loginbutton").Click
End Sub

Not able to click on submit button using vba

When i go to fatch data using (from date) to (to date), click on show button using vba not working
Also i need suggestion, How can i download csv file from multiple dates
i wanted to download csv file for each date between date range
Dim MP, FD, TD As String
MP = ActiveWorkbook.Name
FD = Sheets("Sheet1").Range("XFA2")
TD = Sheets("Sheet1").Range("XFB2")
Dim IE As New SHDocVw.internetexplorer
Dim HTMLDoc As MSHTML.HTMLDocument
IE.Visible = True
IE.navigate "https://www.mcxindia.com/market-data/bhavcopy"
ShowWindow IE.hwnd, SW_SHOWMAXIMIZED
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.document
For N = FD To TD
Workbooks(MP).Worksheets("Sheet1").Range("XEX2").Value = (FD)
N1 = Workbooks(MP).Worksheets("Sheet1").Range("XEX3").Value
HTMLDoc.getElementById("txtDate").Value = (N1)
HTMLDoc.getElementById("btnShowDatewise").Click
Next N
Suggest me the codes to click on submit button and how to download multiple date files,since show button takes time to publish data and then i could click on csv tab on rightside penal of webpage
If you are only setting the one day you can do the following which includes a check that the date produces a download file
Option Explicit
Public Sub Download()
Dim ie As New InternetExplorer
With ie
.Visible = True
.Navigate2 "https://www.mcxindia.com/market-data/bhavcopy"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
.parentWindow.execScript "document.querySelector('#cph_InnerContainerRight_C001_txtDate_hid_val').value='20190201';"
.parentWindow.execScript "document.querySelector('#txtDate').value='20190201';"
.querySelector("#btnShowDatewise").Click
If .querySelectorAll("#cph_InnerContainerRight_C001_lnkExpToCSV").Length > 0 Then
.parentWindow.execScript "__doPostBack('ctl00$cph_InnerContainerRight$C001$lnkExpToCSV','');"
End If
End With
While .Busy Or .readyState < 4: DoEvents: Wend
'.Quit
End With
End Sub

VBA to click image placed in a html table

I have been trying to write excel VBA code that clicks on an image that has been placed in an HTML Table. The image which I am trying to click has only the source code.
I Tried in 4 different ways to click the image, but none of these worked. Can anyone please help me how to write VBA code?
1.
Dim HTMLDoc As Object
Set HTMLDoc = IE.document
Dim ElementCol As Object
Set ElementCol = HTMLDoc.getElementsByTagName("td")
For Each link In ElementCol
If link.innerHTML = "excel.jpg" Then
link.Click
Exit For
End If
Next link
2.
Dim HTMLDoc As Object
Set HTMLDoc = IE.document
Dim i As Object
For Each i In HTMLDoc.images
If i.src = "../images/excel.jpg" Then
i.Click
Exit for
End If
Next i
3.
Dim HTMLDoc As Object
Set HTMLDoc = IE.document
Dim img As Object
For Each img In HTMLDoc.all
If img.innerHTML = "excel.jpg" Then
img.Click
Exit For
End If
Next img
4.
Dim HTMLDoc As Object
Set HTMLDoc = site.document
Dim tdCollection As Object
Set tdCollection = HTMLDoc.all
For Each cell In tdCollection
If cell.ID = "singleXLS" Then
cell.Click
Exit Sub
End If
Next
Here is the HTML code.
<tbody><tr>
<td id="singleXLS" onclick="javascript:gCognosViewer.getRV().viewReport('spreadsheetML');" return="" true;"="" style="cursor:hand;" valign="middle" align="center">
<img src="../images/excel.jpg" border="0"></td>
<td> </td>
As suggested, here is the code I am using.
Sub click_img()
Dim URL As String
Dim HTMLDoc As Object
Set site = CreateObject("internetexplorer.application")
URL = Range("B2").Value 'There is an URL in cell B2
site.navigate URL
site.Visible = True
While site.busy
Wend
Do Until site.readyState = 4
DoEvents
Loop
Set HTMLDoc = site.Document
HTMLDoc.getElementById("singleXLS").getElementsByTagName("img")(0).Click 'Error is here
End Sub
Instead of this:
Set HTMLDoc = site.Document
HTMLDoc.getElementById("singleXLS").getElementsByTagName("img")(0).Click
Try:
Dim s, i
Set HTMLDoc = site.Document
Set s = HTMLDoc.getElementById("singleXLS")
If Not s Is Nothing Then
Set i = s.getElementsByTagName("img")(0)
If Not i Is Nothing Then
i.Click
Else
Debug.Print "Img not found!"
End If
Else
Debug.Print "singleXLX not found!"
End If
A lot more code, but it will help you pin down the exact problem

Using Excel VBA to click through hover dropdown menus

How do we select dropdown option using VBA?
For example, my HTML source code looks like below, and I try to select second one, "코스피200 선물".
<select name="isu_cd" id="isu_cdc74d97b01eae257e44aa9d5bade97baf">
<option value="ALL">전체</option>
<option value="KRDRVFUK2I">코스피200 선물</option>
....
</select>
Therefore, I try to iterate them using "For Each" after selecting that has name="isu_cd".
But the test for "MsgBox (botton.innnerHTML)" in the "For Each" gives me full of HTML code includes every other options as unexpected.
Could we iterate this with my approach?
Sub test_javascript()
url = "http://marketdata.krx.co.kr/mdi#document=060101"
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate url
Do Until (ie.readyState = 4 And Not ie.Busy)
DoEvents
Loop
Application.Wait (Now + TimeValue("00:00:10"))
Set Buttons = ie.document.getElementsByName("isu_cd")
For Each botton In Buttons
MsgBox (botton.innerHTML) 'it gives all of HTML code unexpectedly
If botton.innerHTML = "코스피200 선물" Then 'Nothing comes to here
MsgBox (botton.innerHTML)
botton.FireEvent ("onchange")
Exit For
End If
Next
End Sub
The problem is solved!
Buttons(0).Value = "KRDRVFUK2I" changes the dropdown box into what I desired.
The full solution is in below.
Sub test_javascript()
url = "http://marketdata.krx.co.kr/mdi#document=060101"
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate url
Do Until (ie.readyState = 4 And Not ie.Busy)
DoEvents
Loop
Application.Wait (Now + TimeValue("00:00:05"))
Set Buttons = ie.document.getElementsByName("isu_cd")
Buttons(0).Value = "KRDRVFUK2I"
Set goandfind = ie.document.getElementsByClassName("btn-board btn-board-search")(0)
goandfind.Click
Do Until (ie.readyState = 4 And Not ie.Busy)
DoEvents
Loop
Application.Wait (Now + TimeValue("00:00:05"))
Set buttons_excel = ie.document.getElementsByTagName("button")
For Each botton In buttons_excel
If botton.innerHTML = "Excel" Then
MsgBox (botton.outerHTML)
botton.FireEvent ("onclick")
Exit For
End If
Next
' do whatever
End Sub

Fill user name and password in a webpage using VBA

This is my first try to navigate a IE browser through VBA.
I am trying to:
- go to this webpage https://hb2.bankleumi.co.il/e/Login.html
- fill in the username
- fill the password
- click the "login" button
for now I'm getting the error "Method 'Document' of object 'IWebBrowser2' failed"
I tried to inspect the elements in the html code of the webpage and found their ids but I suppose I'm making some mistake in invoking the methods or approaching the object.
my code is:
Option Explicit
Sub dataFromLeumi()
Dim myPassword As String
myPassword = "somepassword"
Dim myUserName As String
myUserName = "someusername"
Dim loginPath As String
loginPath = "https://hb2.bankleumi.co.il/e/Login.html"
Dim IE As Object
Set IE = CreateObject("InternetExplorer.application")
IE.Visible = True
IE.navigate loginPath
Dim userName As Object
Set userName = IE.document.getattribute("uid")
userName.Item(0).Value = myUserName
Dim password As Object
password = IE.document.getelementbyid("password")
password.Item(0).Value = myPassword
IE.document.getelementbyid("enter").Click
End Sub
What should I change in my code? What do I miss out?
Thanks!
Try this
Sub test()
' open IE, navigate to the desired page and loop until fully loaded
Set ie = CreateObject("InternetExplorer.Application")
my_url = "https://hb2.bankleumi.co.il/e/Login.html"
With ie
.Visible = True
.Navigate my_url
.Top = 50
.Left = 530
.Height = 400
.Width = 400
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
End With
' Input the userid and password
ie.Document.getElementById("uid").Value = "testID"
ie.Document.getElementById("password").Value = "testPW"
' Click the "Search" button
ie.Document.getElementById("enter").Click
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
End Sub

Categories