Input text to search box on web page and click Search - html

I am trying to input data from Excel workbook to web pages search field and click search button. A lot of different approaches has been tested with success. Current solution gives 424 Error, Object required. Pointing to:
.Document.getElementById("crmGrid_findCriteria").Value = _
Sheets("Other data").Range("A2").Value
HTML for search field:
<input id="crmGrid_findCriteria" title="Search for records" hintlabelid="crmGrid_findHintText" type="text" class="ms-crm-Dialog-Lookup-QuickFind" maxlength="100" tabindex="0" value="">
HTML for Search button:
<img id="crmGrid_findCriteriaImg" src="/_imgs/imagestrips/transparent_spacer.gif" class="ms-crm-ImageStrip-search " imgbase="/_imgs/search" title="Start search" alt="Start search">
Here is structure:
Here is my VBA (link for CRM system so it is not public):
Sub GoToOpport()
'This will load a webpage in IE
Dim ieappIE As InternetExplorerObject
Dim HWNDSrc As Long
Dim elementsSet AsappIE Object= CreateObject("internetexplorer.application")
Set ie = Nothing With appIE
Set ie = New InternetExplorerMedium
ie.Visible = True
ie.Navigate "https://crm.dynamics.com/main.aspx?area=nav_oppts&etc=3&page=SFA&pageType=EntityList&web=true"
With ie
Do
DoEvents
Loop Until ie.ReadyState = READYSTATE_COMPLETE
End With
ie.Document.getElementById("contentIFrame0").contentDocument.getElementById("crmGrid_findCriteria").Value = "hello"
'Unload IE
Set ieappIE = Nothing
End Sub
Error:

It is in an iframe so you need to access that
ie.document.getElementById("contentIFrame0").contentDocument.getElementById("crmGrid_findCriteria")
You can use js to attempt to assign value
ie.document.parentWindow.execScript "document.getElementById('contentIFrame0').document.getElementById('scrmGrid_findCriteria').value = 'hello';"
You could also try navigating to the src of the iframe
ie.navigate2 ie.document.querySelector("#crmContentPanel").src
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
With ie.document.getElementById("crmGrid_findCriteria")
.focus
.value = "abc"
End With

I'm getting a 301 Moved Permanently status when I send a GET request and then I'm redirected here:
https://dynamics.microsoft.com/en-us/crm/what-is-crm/
The id="crmGrid_findCriteria" is nowhere to be found in the HTML response, so my guess is that if you try:
debug.print ie.Document.getElementById("crmGrid_findCriteria") Is Nothing
you'll get TRUE as a result.
In other words, the element you're looking for doesn't exist in the HTML you downloaded.

Related

How to click the list item in web IE application with VBA

Basically my requirement is to download the excel file from webpage through vba. I have written the code to login but unable to click on the list item. I need to iterate the loop through class and then list and tag a. So help me how can iterate through multiple option with vba.
I have attached the HTML source code i need to access hourly report from first list under Ul tag.
Sub DownloadIntraDayReport()`
Dim ie As New InternetExplorer
ie.navigate "weblink"
ie.Visible = True
While ie.Busy DoEvents Wend
Do Loop Until ie.readyState = READYSTATE_COMPLETE
Set HTMLDoc = ie.document
ie.document.getElementById("_58_login").Value = "username"
ie.document.getElementById("_58_password").Value = "password"
'ie.document.form().submit
For Each MyHTML_Element In HTMLDoc.getElementsByTagName("input") If MyHTML_Element.Type = "submit" Then MyHTML_Element.Click
Next
End Sub
Source Code
Image of page and HTML
Have you tried something like:
IE.document.getElementById("scheduledReportList").Click
Where IE is the instance of Internet Explorer being used to scrape the page.

Interaction With WebPage VBA

I'm creating a code that allows me to open a specific site and enter value in element with the name searchByTagName=searchByTRSMP and then Type search to load the new window
But the problem that button search doesn't have a TagName or IdName only this
<td width="13%" align="center" rowSpan="1" colSpan="1">
<input name="" onclick="Javascript:document.forms[0].submit();return false;" type="image" srx="/cmh/cmh/xxxxxx" border="0"></input>
Anyone Can Light me on pressing that button with only those conditions
this Mycode :
Sub ToComb()
Dim ie As Object
Dim itm As IHTMLElement
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "http://XXXX-
XXXX.eu.airbus.XXXXXp:XXXXX/cmh/consultation/preSearchTRSTDMAS.do?
clearBackList=true&CMH_NO_STORING_fromMenu=true"
While ie.Busy Or ie.readyState <> 4: DoEvents: Wend
Set itm = ie.document.getElementsByName("searchByTRSMP")(0)
If Not itm Is Nothing Then itm.Value = "k20734"
Set Doc = ie.document
Set tags = ie.document.getElementsByTagName("")
For Each tagx In tags
If tagx.Value = "Next" Then
tagx.Click
Exit For
End If
Next
End Sub
GetElementsByTagname mean search for an element by the type of an HTML element (such as, div, p or in your example - input).
You can get all your inputs tags (elements), iterate them and identify the required input, based on it's (for example) srx attribute:
Set tags = ie.Document.GetElementsByTagname("Input")
For Each tagx In tags
If tagx.src= "/cmh/cmh/xxxxxx" Then
tagx.Click
End If
Next
In addition, the final src of the input might changed from the actual code, because you use a relative path. Check the actual src with a MsgBox:
For Each tagx In tags
MsgBox tagx.src
I assume it will be different, such as prefix of http and so on:
If tagx.src = "http://xxxx
xxxx.eu.airbus.xxx:xxxx/cxxx/xxx/image/button_search.gif" Then

HTTPS certificate error excel VBA

Tried to look into the old questions, but couldn't find the answer I was looking for.
I'm trying to make a spreadsheet that automatically scrapes the toner levels in our printers - however, there is a certificate error.
Instead of posting the website, IE (and therefore Excel/VBA) shows the cert.error HTML.
How can I bypass this? It is not possible to add the site to trusted sites - can VBA somehow ignore the error, or can I make VBA press tab, enter?
I've tried to use the following code, however no luck.
Enum readystate
READYSTATE_UNINITIALIZED = 0
READYSTATE_LOADING = 1
READYSTATE_LOADED = 2
READYSTATE_INTERACTIVE = 3
READYSTATE_COMPLETE = 4
End Enum
Sub ImportPrinterData()
Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = False
ie.navigate "https://to-port3-hp1c/"
Do While ie.readystate <> READYSTATE_COMPLETE
Application.StatusBar = "Venter på printer data ..."
DoEvents
Loop
Set html = ie.document
Application.SendKeys "{tab 7}", True
Application.SendKeys "{return}", True
MsgBox html.DocumentElement.innerHTML
Set ie = Nothing
Application.StatusBar = ""
End Sub

VBA Scraping with IE11 - HTML Elements not indexing in VBA Local - Call javascript instead?

I've been scraping away for the last couple months with no issues, but now I have a site I'd like to access that is a bit more complicated than simple HTML. When I inspect elements, I can find the tag, ID, and name of an input box i'd like to fill, but when I run the code, there are no elements within the VBA locals window for my ie object.
I believe this is due to AJAX, but i'm too novice with ajax/javascript to really know for sure.
This is a screenshot of the input box i'd like to fill out:
and this is the code I'm using:
Sub Findie()
Dim ie As Object
Dim userName As Variant
Dim passWord As Variant
Dim strHTML As String
Dim sht As Worksheet
Dim itemRng As Range
Dim testObj As Object
Set sht = Sheets("Sheet1")
Set itemRng = sht.Range("A2")
userName = "***#***.com"
passWord = "********"
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
strHTML = "http://[MYWEBSITE].com"
ie.Navigate strHTML
'wait for browser
While ie.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
Application.Wait (Now + #12:00:03 AM#)
ie.Document.getElementsByName("username")(0).Value = userName
ie.Document.getElementsByName("password")(0).Value = passWord
ie.Document.getElementsByClassName("submit")(0).Click
While ie.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
Application.Wait (Now + #12:00:01 AM#)
ie.Navigate "[MYWEBSITE]" 'after logging in, navigating to this page saves time
While ie.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
ie.Document.getElementsByName("productId")(0).Value = "12345"
This is not an issue with the page loading either, it does not work when I step through.
I'm thinking it will be much easier if I can actually run the scripts that are on the server, but I've never done that using VBA. I'm not really sure how. Here is a screenshot of the scripts available on the server. I don't really know where to go from here, or which method is ideal.

Problems Using VBA to Submit a Web Page - Using the click button function but web page won't submit

I am writing a VBA code to pull data from a website (https://app.buzzsumo.com/top-content). I have a functional code that runs without errors however I still can't get the webpage to actually submit the form when the click command runs. I have tried many different approaches and combinations of submitting the form/clicking the submit button but none have seemed to work so far. Below is my current code.
Sub clickFormButton()
Dim ie As Object
Dim form As Variant,
Dim button As Variant
'add the “Microsoft Internet Controls” reference in VBA Project
Set ie = CreateObject("InternetExplorer.Application")
'using input box to enter URL I am serching for
Search_URL = InputBox("Enter URL to Search For")
With ie
.Visible = True
.navigate ("https://app.buzzsumo.com/#/top-content")
'Ensure that the web page downloads completely
While ie.ReadyState <> 4
DoEvents
Wend
'assigning the input variables to the html elements of the form
ie.document.getElementsByName("q").Item.innertext = Search_URL
'finding and clicking the button
Set objInputs = ie.document.getElementsByTagName("input")
For Each ele In objInputs
If ele.Title Like "Press Enter to Search" Then
ele.Click
End If
End With
End Sub
I have also tried other methods to find and click the button such as:
'Dim i As Variant
'Set form = ie.document.getElementsByClassName("btn btn-highlight")
'For i = 1 To 5
'If form.Item(i).DefaultValue = "Search!" Then
'Set button = form.Item(i)
'button.Click
'End If
'Next i
Please provide any recomendations on what I may be missing or how I can get this code to actually submit the form and advance to the search results. Thanks in advance for any help you can provide!
Here are some additional details: Unfortunately the element I am trying to click (the "Search" button) does not have an ID or Name associated with it. This is why is was trying alternative approaches, such as looping through all of the object and trying to find the one with the right “Title”. Here is the code for the element from the DOM explorer:
<input title="Press Enter to search" class="btn btn-highlight" type="submit" ng-disabled="topContentSearchForm.$invalid" value="Search!"/>
The only attributes associated with it are:
class: btn btn-highlight
type: submit
ng-disabled: topContentSearchForm.$invalid
value: Search!
title: Press Enter to Search
Please let me know if there is another way to find the element ID/name? or if there is another way to click the button without these attributes? Thanks
I know this is an old post but... I have been using this effectively..
'click login
Set htmlDoc = .document
Set htmlColl = htmlDoc.getElementsByTagName("input")
Do While htmlDoc.readyState <> "complete": DoEvents: Loop
For Each htmlInput In htmlColl
If Trim(htmlInput.Type) = "submit" Then
htmlInput.Click
Exit For
End If
Next htmlInput
A couple of ideas:
While ie.ReadyState <> 4
DoEvents
Wend
If you have javascripts on the page use Application.Wait Now + TimeSerial(0, 0, 4) (basically wait for 4 seconds) instead.
Second I don't understand why you need to loop through all the objects on the web page. The easier way would be to go that webpage in IE, hit F12 and select element in DOM explorer, you can get the ID or Name of the button and then use ie.document.GetElementByID("buttonID").Click or ie.document.GetElementsByName("buttonName").Item.Click
Let me know if this helps.
Edit: After inspecting the particular webpage it appears that the ID and Name attributes for that button are missing. So I had to resort to the following:
Dim i As integer
Set form = ie.document.getElementsByClassName("btn btn-highlight")
On Error Resume Next
For i = 1 To 20
If form.Item(i).DefaultValue = "Search!" Then
form.Item(i).Click
End If
Next i
The relevant button is clicked for the fourth item (I had to manually go through the loop because 3rd item navigated away from the page to a pricing page, so i had to go back). Anyway the full code is the following, please note that you will need to go through this exercise again if there were changes to the webpage
Sub clickFormButton()
Dim ie As Object
Dim form As Variant
Dim button As Variant
'add the “Microsoft Internet Controls” reference in VBA Project
Set ie = CreateObject("InternetExplorer.Application")
'using input box to enter URL I am serching for
Search_URL = InputBox("Enter URL to Search For")
With ie
.Visible = True
.navigate ("https://app.buzzsumo.com/#/top-content")
End With
'wait for page to load
Application.Wait Now + TimeSerial(0, 0, 5)
'assigning the input variables to the html elements of the form
ie.document.getElementsByName("q").Item.InnerText = Search_URL
'finding and clicking the button
ie.document.getElementsByClassName("btn btn-highlight").Item(4).Click
End Sub
It looks like you could potentially just build the string URL, for example if you put "abcd" in the search field, the resulting URL will be:
https://app.buzzsumo.com/top-content?result_type=total&type=articles&num_days=360&tfc=false&general_article&infographic&video&page=1&guest_post&giveaway&interview&links_sitewide=true&unique_domains=true&backlinks=false&q=abcd&offset=0
Note the bolded portion which is the search query.
So, and this is just a quick idea that may work as long as you're not trying to abuse their system by sending 1000's of automated requests:
Sub FetchWebsite()
Dim ie As Object
Dim form As Variant
Dim button As Variant
Dim url As String
'add the “Microsoft Internet Controls” reference in VBA Project
Set ie = CreateObject("InternetExplorer.Application")
'using input box to enter URL I am serching for
Search_URL = InputBox("Enter URL to Search For")
'### BUILD THE FULL URL
url = "https://app.buzzsumo.com/top-content?result_type=total&type=articles&num_days=360&tfc=false&general_article&infographic&video&page=1&guest_post&giveaway&interview&links_sitewide=true&unique_domains=true&backlinks=false&q=" & Search_URL & "&offset=0"
With ie
.Visible = True
.navigate url
End With
'wait for page to load
Do
Loop While Not ie.ReadyState = 4 And Not ie.Busy
AppActivate "Internet Explorer"
End Sub
I did some poking around in the Locals window and this should also work, modified from your code. This would be the Form.Submit that I mentioned in comment on OP.
Sub clickFormButton()
Dim ie As InternetExplorer
Dim form As Variant
Dim button As Variant
Dim ele As HTMLFormElement
'add the “Microsoft Internet Controls” reference in VBA Project
Set ie = CreateObject("InternetExplorer.Application")
'using input box to enter URL I am serching for
Search_URL = InputBox("Enter URL to Search For")
With ie
.Visible = True
.navigate ("https://app.buzzsumo.com/#/top-content")
End With
'wait for page to load
Do
Loop While Not ie.ReadyState = 4 And Not ie.Busy
'assigning the input variables to the html elements of the form
ie.document.getElementsByName("q").Item.InnerText = Search_URL
'finding and clicking the button
ie.document.getElementsByClassName("btn btn-highlight").Item(4).form.submit
End Sub
CSS selector:
You can use CSS selector of #search-btn > div. Which is div within className search-btn. "#" means class.
VBA:
Use .querySelector method to apply CSS selector:
ie.document.querySelector("#search-btn > div").Click