When the search button is clicked using vba the text entered in search box is not seen by web page

vrsharma

New Member
Joined
Aug 4, 2019
Messages
18
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi All,
I have written vba code for entering manufacturer part number in search box of below website and clicking on search icon. It is able enter manufacturer part number in search box and click on search icon, but when "search icon is clicked the text entered in the text box is not picked up". It searches empty data.
It being almost a month I have tried various different way but nothing worked. Kindly requested you all to please help me on this.

<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">'HTML Part for search icon

<a href="javascript:void(0);" style="float: right; width: 20%; height: 55px; border-top-right-radius: .25rem!important; border-bottom-right-radius: .25rem!important; border: 0; padding: 0!important; background: #dadada ; margin: 0!important; display: flex; align-items: center;justify-content: center;text-decoration: none;" ng-click="SearchButt*******(1)"><em class="fa fa-search" aria-hidden="true" style="color: gray;"></em></a></code>

<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">' VBA code
Sub AptivScrapping()

Dim IE As SHDocVw.InternetExplorer
Set IE = New InternetExplorer
IE
.Visible = True

IE
.navigate "https://ecat.aptiv.com"

Do While IE.readyState < READYSTATE_COMPLETE
Loop

Dim idoc As MSHTML.HTMLDocument
Set idoc = IE.document

idoc
.getElementById("searchUserInput").Value = "33188785"

Dim doc_ele As MSHTML.IHTMLElement
Dim doc_eles As MSHTML.IHTMLElementCollection

Set doc_eles = idoc.getElementsByTagName("a")

For Each doc_ele In doc_eles
If doc_ele.getAttribute("ng-click") = "SearchButt*******(1)" Then
doc_ele
.Click
Exit Sub
Else
End If
Next doc_ele

End Sub</code>
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You probably need to send events when inputting the search string to emulate what the page does when manually inputting the search string.

Using your browser's DOM Inspector, you'll see that the text input element ("searchUserInput") has the following events: blur, change, compositionend, compositionstart, input, keydown, keypress. And the parent form has the following events: click, submit.

Try adapting the code at https://www.mrexcel.com/forum/excel...larjs-input-text-post5317832.html#post5317832, which shows how to emulate the events on an element using dispatchEvent. As shown in that code, you probably need to enter the search string character by character and send keydown and/or keypress and input events inside the loop, and maybe the change event at the end. The search button may work with the simple Click method (per your code), or you may need to send a click event instead.
 
Upvote 0
Hi John Thank you so much. :) I went to given link in your comment and I applied the mentioned method and its worked for me.

Below is the changes in my code which I have done.


Sub AptivScrapping()


Dim IE As SHDocVw.InternetExplorer
Set IE = New InternetExplorer
IE.Visible = True

IE.navigate "https://ecat.aptiv.com"

Do While IE.readyState < READYSTATE_COMPLETE
Loop

Dim idoc As MSHTML.HTMLDocument
Set idoc = IE.document

IE.document.getElementById("searchUserInput").Focus = True
IE.document.getElementById("searchUserInput").Select

sFieldInput = "33188785"
For s = 1 To Len(sFieldInput)
Application.SendKeys Mid(sFieldInput, s, 1)
While IE.readyState < 4 Or IE.Busy
Application.Wait DateAdd("s", LoopSeconds, Now)
Wend
Next s


IE.document.getElementById("searchUserInput").Focus = False 'De-focus the field and cause change event to trigger

Dim doc_ele As MSHTML.IHTMLElement
Dim doc_eles As MSHTML.IHTMLElementCollection

Set doc_eles = idoc.getElementsByTagName("a")

For Each doc_ele In doc_eles
If doc_ele.getAttribute("ng-click") = "SearchButt*******(1)" Then
doc_ele.Click
Exit Sub
Else
End If
Next doc_ele


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top