not able to pick value from webpage and get it into excel - html

I am new to VBA,
I want some data from webpage into excel sheet..
I have written code for visiting the web page, input value and clicking on submit button.
On clicking submit button, next webpage comes.. I want the emailID from that webpage to my excel sheet.. Please help
the code i have written is as follows:
Sub emailforcform()
Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate "https://registration.apct.gov.in/ctdportal/Search/EMailSearch.aspx"
Do While ie.readyState <> READYSTATE_COMPLETE
Application.StatusBar = "Trying to connect"
DoEvents
Loop
Set html = ie.document
ie.document.all("ctl00$ContentPlaceHolder1$TxtTin").Value = "28740213505"
ie.document.all("ctl00$ContentPlaceHolder1$ButGet").Click
Range("c4").Value = ie.document.all("ContentPlaceHolder1_UpdatePnl").Value
End Sub

This works for me...
Sub test()
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "https://registration.apct.gov.in/ctdportal/Search/EMailSearch.aspx"
Application.StatusBar = "Trying to connect"
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
ie.Document.all("ctl00$ContentPlaceHolder1$TxtTin").Value = "28740213505"
ie.Document.all("ctl00$ContentPlaceHolder1$ButGet").Click
Application.StatusBar = "Preparing to retrieve the email ID"
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
Range("c4").Value = ie.Document.getElementsByTagName("tbody")(7).getElementsByTagName("tr")(1).getElementsByTagName("td")(1).innertext
end sub

Related

Entering text into webpage searchbox

I am trying to enter text into a searchbox and am running into different errors. Below is my code, can anyone point out to me where i have gone wrong?
Sub GetHTMLDocument()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
IE.Visible = True
IE.navigate "http://shopee.sg"
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.document
Set HTMLInput = HTMLDoc.getElementsByClassName("shopee-searchbar-input__input")
HTMLInput.Value = "Excel VBA"
End Sub
I suggest try to make a test with the code sample below may help to solve the issue.
Sub demo()
Dim URL As String
Dim IE As Object
Dim element As HTMLInputElement
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "YOUR WEB PAGE ADDRESS HERE" 'Add your site address here....
IE.navigate URL
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Application.Wait DateAdd("s", 2, Now)
Set element = IE.document.querySelector("[class='shopee-searchbar-input__input']")
element.removeAttribute ("aria-label")
element.removeAttribute ("placeholder")
element.Value = "abc"
element.FireEvent ("OnChange")
Set IE = Nothing
End Sub
Output:
Further, you can modify the code example as your requirements.

How to copy/paste data from excel to a webpage (VBA)?

So I'm trying to copy/paste data from excel into a webpage text box using VBA. However, my problem is that if I have, for example 3 or 4 rows of data copied, when pasting the values into the webpage using vba, only 1 row will be copied rather than all the rows.
Here is my code:
.Document.getElementsByTagName("textarea")(0).Value = ActiveCell.Value
Any ideas? If I take out the (0) I get an error:
object doesn't support this property or method.
This is some code of mine that works:
Sub FillOutInvoice(Account As Account)
Dim ie As InternetExplorer
Dim elem As HTMLLinkElement
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate Settings.ErpAddress
Do While ie.Busy Or ie.readyState <> 4
DoEvents
Loop
With ie.document
.getElementsByClassName(Settings.InputClass)(0).innerText = Account.InvoiceNumber
.getElementsByClassName(Settings.InputClass)(1).innerText = Day(Account.InvoiceDate)
End With
Do While ie.Busy Or ie.readyState <> 4
DoEvents
Loop
Application.Wait Now + #12:00:02 AM#
ie.Quit
End Sub
As you see, the needed property is .InnerText and not .Value.
Here is an example of sending a range of rows text to a textarea element using the clipboard
Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub InsertData()
Dim ie As New InternetExplorer
With ie
.Visible = True
.Navigate2 "https://www.google.com/search?q=google+translate&rlz=1C1GCEB_enGB815GB815&oq=google+tran&aqs=chrome.0.0j69i57j0l4.2057j0j7&sourceid=chrome&ie=UTF-8"
While .Busy Or .readyState < 4: DoEvents: Wend
Dim clipboard As Object
Set clipboard = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
ActiveSheet.Range("A1:A3").Copy
With clipboard
.GetFromClipboard
ie.document.getElementsByTagName("textarea")(1).innerText = .GetText
End With
Application.CutCopyMode = False
Stop
.Quit
End With
End Sub
Contents of Range("A1:A3")

VBA wait for everything in a webpage to completely load

I'm trying to pull some information from a website after navigating to it but I can't seem to wait until it completely loads. I've been trying to loop until the class at (0) contains text. Anyone know what I'm doing wrong?
Sub test()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
Dim elements2 As IHTMLElementCollection
IE.Navigate "https://www.facebook.com/marketplace/item/955559644646354/"
Do While IE.Busy Or IE.readyState <> 4
DoEvents
Loop
Dim x
x = 0
Do Until x = 1
Set elements2 = IE.document.getElementsByClassName("_3cgd")
If WorksheetFunction.IsText(elements2(0).innerText) = True Then
MsgBox ((elements2(0).innerText))
x = 1
Else
Application.Wait Now + #12:00:01 AM#
End If
Loop
End Sub
Try something like this (untested)
Dim t, elements2, txt
t = Timer
txt = ""
Do
Set elements2 = IE.document.getElementsByClassName("_3cgd")
If elements2.length > 0 Then
txt = elements2(0).innerText
If Len(txt) > 0 Then Exit Do
End If
If (Timer - t) > 10 Then Exit Do 'exit if too long waiting
Application.Wait Now + TimeSerial(0, 0, 1)
Loop
You can try to add lines below to wait for loading the web page completely.
' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
Do While IE.ReadyState = 4: DoEvents: Loop 'Do While
Do Until IE.ReadyState = 4: DoEvents: Loop 'Do Until
Below is the full working example:
Sub Automate_IE_Load_Page()
'This will load a webpage in IE
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection 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 = "https://www.automateexcel.com/excel/"
'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 inadvertently skipping over the second loop)
Do While IE.ReadyState = 4: DoEvents: Loop 'Do While
Do Until IE.ReadyState = 4: DoEvents: Loop 'Do Until
'Webpage Loaded
Application.StatusBar = URL & " Loaded"
'Unload IE
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
End Sub
Further, You can try to modify this code example as per your requirement.
Reference:
(1) Automate Internet Explorer (IE) Using VBA

VBA Web Automation on a dynamic site

I am trying to scrap barcode owners from this site : http://gepir.gs1.org/index.php/search-by-gtin
I am receiving "Object doesnt support this property or method" error msg because of the final line. I tried to extract it with class and tag but failed.
I am trying to extract the company name from the dynamic table.
Sub aa()
Dim ie As New SHDocVw.InternetExplorer
Dim doc As HTMLDocument
Dim aaa As Object
Dim objIE As InternetExplorer
Set objIE = New InternetExplorer
ie.Visible = True
ie.navigate "gepir.gs1.org/index.php/search-by-gtin"
Do While ie.readyState <> READYSTATE_COMPLETE
Loop
Debug.Print ie.LocationName, ie.LocationURL
Set doc = ie.document
ie.document.forms("searchbygtin").elements("keyValue").Value = "685387379712"
ie.document.forms("searchbygtin").elements("method").Click
Debug.Print ie.document.getElementsBytag("tr")(7).innerText
End Sub
Any help would be great,
Thanks
You need TagName
Debug.Print ie.document.getElementsByTagName("tr")(7).innerText
I get a captcha though so I doubt you can complete this task in this way. Selenium and IE both seem to trigger the captcha, at least for me.
If not for the captcha I would re-write your script as follows (perhaps with a loop to make sure tr element is present):
Option Explicit
Public Sub GetLastChangeDate()
Dim ie As New SHDocVw.InternetExplorer, doc As HTMLDocument
Const URL = "gepir.gs1.org/index.php/search-by-gtin"
With ie
.Visible = True
.navigate URL
While .Busy Or .readyState < 4: DoEvents: Wend
Set doc = .document
Debug.Print .LocationName, .LocationURL
doc.getElementById("keyValue").Value = "685387379712"
doc.getElementById("submit-button").Click
While .Busy Or .readyState < 4: DoEvents: Wend
Debug.Print .document.getElementsByTagName("tr")(7).innerText
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