VBA web scraping automating contents in HTML document without ID and classname - excel

I am trying to automate a process in which entry from a excel cell will be entered in to web form and resulting data will be collected in separated excel cell. However, the webpage HTML doesnt have class or ID. it is run through a jss script. Below is the HTML code -
I am stuck with following VBA code -
Sub updateSRV()
Dim ie As InternetExplorer
Dim doc As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = True
URL = "http://hwrapp:7778/reports/jsprep/umesh/cnote/cnote_query5.html"
ie.navigate URL
Do Until ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
ie.document.getElementsByTagName$("input[name=cntno]").Value = 20220064
End Sub

Related

Click on Class names in VBA - No Selenium

I am into Project Management with no knowledge of IT / Coding.
I am trying to do web automation.
It will have to click on certain links, submit a form and then hit save.
However I am not able to click on the first link as well.
this is my code as of now.
Sub CommandButton1_Click()
Dim ie As Object
Dim html As HTMLDocument
Dim form As Variant, button As Variant
Sheet1.Range("B6").Value = Application.UserName
'Open Internet Explorer
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "https://www.link.com/"
ie.Visible = True
While ie.Busy
DoEvents
Wend
Set html = ie.document
html.getElementsByClassID("p2205").Click
End Sub
Below is the code.
I want to click on Company Name.
Website Code
I have tried the web and YouTube videos as well but I guess I don't know the language or the logic behind coding. Hence I am not able to get through.
Add reference to Microsoft Internet Controls then try
Option Explicit
Sub CommandButton1_Click()
Dim ie As Object, html As HTMLDocument, e As HTMLHtmlElement
Dim URL As String
URL = "https://www.link.com/"
Set ie = New InternetExplorerMedium
ie.navigate URL
ie.Visible = True
While ie.Busy
DoEvents
Wend
Set html = ie.document
For Each e In html.getElementsByClassName("dsh_sta_301")
If InStr(e.onclick, "projectSelection") > 0 Then
e.Click
End If
Next
End Sub

Submitting form and reading results using Excel VBA and InternetExplorer

I'm submitting a form using Excel VBA while using an InternetExplorer object. Once submitted, I can see the URL change on screen. However, when I attempt to output the URL (to confirm that it changed and the code knows it), I get the same URL.
In both debug statements below, they output the same URL.
Code:
Dim username As String
Dim password As String
Dim server_ip As String
username = "aaa"
password = "bbb"
server_ip = "ip_here"
Dim ie As New InternetExplorer
Dim doc As HTMLDocument
Set doc = New MSHTML.HTMLDocument
Dim url As String
ie.Visible = True
ie.navigate "my_url"
'wait
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
Set doc = ie.document
Debug.Print "url: " & doc.url ' is /abc.html
'set credentials
doc.all.username.Value = username
doc.all.password.Value = password
'submit
ie.document.getElementsByTagName("form")(0).submit
Debug.Print "submitted..."
'wait
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
Set doc = ie.document
Debug.Print "url: " & doc.url 'should be /def.html, but returns /abc.html
The query on readystate_complete works only once in this way. After that the status remains the same. Therefore you can work with a manual pause if necessary.
'The last three values are hours, minutes, seconds
'This waits for 5 seconds
Application.Wait (Now + TimeSerial(0, 0, 5))
Another way is to wait with a loop until a known html element is found. Look at this example for more information:
Online search bar values after export from excel not clicking automatically tag identify wrong
One more example for using a loop:
Excel VBA - Web Scraping - Inner Text of HTML Table Cell

Engage with widget on IE by VBA

I haven't really tried working on this before, so I have no idea what I am doing at the moment. I have limited knowledge of html so not sure whether I am doing right. Basically what I aim to do is opening the Internet explorer by macro, changing some elements based id and click submit button on the website to show the data. Then I need keep working on the next step.
As you can see from the code I was trying to engage with the widget on IE by id number from html codes.
Sub Automate_IE_Enter_Data()
'This will load a webpage in IE
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objbutton As Object
'Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True
'Define URL
URL = "http://cfpsg1/plant/Reports/ScrapReport.aspx"
'Navigate to URL
IE.Navigate URL
' Statusbar let's user know website is loading
Application.StatusBar = URL & " is loading. Please wait..."
' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertantly skipping over the second loop)
Do While IE.ReadyState = 4: DoEvents: Loop
'Webpage Loaded
Application.StatusBar = URL & " Loaded"
IE.Document.getelementbyid("1stGroupBy").Value = "3"
'Find & Fill Out Input Box
IE.Document.getelementbyid("PageContent_uxStartDate").Value = "06/21/2019"
IE.Document.getelementbyid("PageContent_uxEndDate").Value = "06/21/2019"
Set objbutton = IE.Document.getelementbyid("PageContent_btnQuery")
objbutton.Focus
objbutton.Click
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
End Sub
First thing first the webpage popped up but nothing changed of widgets besides an
error message "method 'Document' of object 'IWebBrowser 2' failed"
show on IE.Document.getelementbyid("1stGroupBy").Value = "3" row.
You are trying to interact with a dropdown so you want syntax such as
IE.Document.querySelector("[value='3']").Selected = True
You could also use
IE.Document.querySelector("#1stGroupBy").SelectedIndex = 2 'change to appropriate index
Error "method 'Document' of object 'IWebBrowser 2' failed" may be due to Integrity level
You can try below mentioned code by changing Integrity level as Medium
Dim IE As InternetExplorer
Set IE = New InternetExplorerMedium
IE.Visible = True
URL = "http://cfpsg1/plant/Reports/ScrapReport.aspx"
IE.Navigate URL
Do While IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
and try other line of codes to avoid this issue also please refer Here
Please add ref Microsoft Internet Controls and Microsoft HTML Object Library based on your req

Extract html source code into excel using VBA

I am trying to simply paste the content or innertext into excel using getElementByID function.
The content is actually the iframe link which I am trying to extract it and paste into cell.
The photo shown is the html source code.
Sub GetData()
Dim ie As New SHDocVw.InternetExplorer
Dim htmldoc As MSHTML.HTMLDocument
Dim result As MSHTML.IHTMLElement
ie.Visible = True
ie.navigate "http://www.bursamalaysia.com/market/listed-companies/company-announcements/5925865"
Do While ie.readyState <> READYSTATE_COMPLETE
Loop
Application.Wait (Now() + TimeValue("00:00:016")) ' For internal page refresh or loading
Set htmldoc = ie.document
Set Results = HTML.getElementById("bm_ann_detail_iframe")
Sheets("Sheet1").Range("a1").Value = Results.innerText
End Sub
html source code
You should use consistent variable naming in your code. If you put Option Explicit at the top of your code that will help.
You want to access the src attribute of the iframe to get the URL shown.
If you plan to use the new URL then you actually want the part before the "#". This means changing to:
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = Split(ie.document.getElementById("bm_ann_detail_iframe").src, "#")(0)
Code:
Option Explicit
Public Sub GetData()
Dim ie As New SHDocVw.InternetExplorer
ie.Visible = True
ie.navigate "http://www.bursamalaysia.com/market/listed-companies/company-announcements/5925865"
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
ThisWorkbook.Worksheets("Sheet1") = ie.document.getElementById("bm_ann_detail_iframe").src
ie.Quit
End Sub

VBA Excel Click on a Label

I am new to Excel VBA. I am currently doing excel IE Automation where I want to click label on webpage.
The HTML source code is:
<td class = t19TabItem>Compliance</td>
Any help to click on label compliance.
I used the below code, has found the label compliance by using inner html, but it does not click on compliance tab. It throws errors as object does not this property.
set link=IE.document.getElementsByTagName("a")
if link.InnerHTML="compliance" and link.href="javascript:apex.submit('D_Price')" then
click link
end if
How to click on compliance label?
try the code with references below
Microsoft HTML Object Library
Microsoft Internet Controls**
Sub test()
Dim oHTML_Element As IHTMLElement
Dim oBrowser As InternetExplorer
Dim ie As Variant
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "your web link" 'Your weblink goes here
While ie.readyState <> READYSTATE_COMPLETE And ie.readyState <> READYSTATE_LOADED
DoEvents
Wend
Application.Wait (Now() + TimeValue("00:00:03"))
For Each oHTML_Element In ie.document.getElementsByTagName("a")
If oHTML_Element.innerHTML = "compliance" Then
oHTML_Element.Click
End If
Next
End Sub

Resources