Search for an item in a search box in a website.

API_newnoob

New Member
Joined
Jun 6, 2020
Messages
21
Office Version
  1. 2016
Platform
  1. Windows
Hi I am new to this, i watched a few tutorials and tried out on my own and realize that i can't even do a search in the search box of the webpage.

Below is my code that i tried.

VBA Code:
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://shopee.sg/"
 
    '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"

    'what i tried.
    'IE.document.getElementById("shopee-searchbar-input").Value = "value"       'Find by ID
    'IE.document.getElementById("shopee-searchbar-input__input").Value = "value"       'Find by ID
    'IE.document.getElementsByTagName("shopee-searchbar-input").Value = "value"        'Find by tag
    'IE.document.getElementsByTagName("shopee-searchbar-input__input").Value = "value"        'Find by tag
    'IE.document.getElementsByClassName("shopee-searchbar-input").Value = "value"      'Find by class
    'IE.document.getElementsByClassName("shopee-searchbar-input__input").Value = "value"      'Find by class
    'IE.document.getElementsByName("shopee-searchbar-input").Value = "value"           'Find by name
    'IE.document.getElementsByName("shopee-searchbar-input__input").Value = "value"           'Find by name
    
    'Unload IE
    Set IE = Nothing
    Set objElement = Nothing
    Set objCollection = Nothing
    
End Sub

I have also upload an image of the webpage and highlight the search box that i want to input a text.

Can someone tell me what i did wrong?
 

Attachments

  • Untitled.gif
    Untitled.gif
    98.4 KB · Views: 7

Some videos you may like

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

VBE313

Well-known Member
Joined
Mar 22, 2019
Messages
669
Office Version
  1. 365
Platform
  1. Windows
The Search TextBox might be in a different frame. I use SideeX in Google Chrome and Record my clicks to find out what Frame I am in.

SideeX Website
 

API_newnoob

New Member
Joined
Jun 6, 2020
Messages
21
Office Version
  1. 2016
Platform
  1. Windows
Dear @John_w

I understand you are knowledgeable in this aspect. Can i get your feedbacks?
 

VBE313

Well-known Member
Joined
Mar 22, 2019
Messages
669
Office Version
  1. 365
Platform
  1. Windows
What about my feedback? You need to find out which frame you are in.
 

VBE313

Well-known Member
Joined
Mar 22, 2019
Messages
669
Office Version
  1. 365
Platform
  1. Windows
I used Selenium Chrome for my Internet Automation... Try this code. I was successful with my
Selenium code below your code.

VBA Code:
Sub Automate_IE_Load_Page()
Dim i As Long, URL As String, IE As Object, objElement As Object, objCollection As Object
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    URL = "https://shopee.sg/"
    IE.navigate URL
    Application.StatusBar = URL & " is loading. Please wait..."
    Do While IE.readyState = 4: DoEvents: Loop
    Do Until IE.readyState = 4: DoEvents: Loop
    Application.StatusBar = URL & " Loaded"
    IE.document.getElementByClass("shopee-popup__close-btn").Click
    IE.document.getElementbyCss("input.shopee-searchbar-input__input").SendKeys "test"
    IE.document.getElementByXPath("//button[@type='button']").Click
    Stop
    Set IE = Nothing
    Set objElement = Nothing
    Set objCollection = Nothing
End Sub


this is what worked for me when using Selenium and Google Chrome. IE is too slow and boring.

VBA Code:
Sub MrExcel()
Dim activeELE As String
Dim bot As New Selenium.WebDriver, ele As WebElement, Xpath As String
bot.Start "chrome", "https://shopee.sg"
bot.Get "/"
bot.FindElementByClass("shopee-popup__close-btn").Click
bot.FindElementByCss("input.shopee-searchbar-input__input").SendKeys "test"
bot.FindElementByXPath("//button[@type='button']").Click
Stop
End Sub
 

API_newnoob

New Member
Joined
Jun 6, 2020
Messages
21
Office Version
  1. 2016
Platform
  1. Windows
Dear @VBE313,

Thank you very much for your efforts and input, i tried it out but unfortunately, there is a run time error 438 on the line "IE.document.getElementByClass("shopee-popup__close-btn").Click" .

Is there something i am missing out?

I tried googling and altering but it just does not work, i get a different run time error 424 or some other errors.

I initially wanted to use selenium to automate google chrome as I too, think that IE is slower.

However, i am unable to download anything from the web and therefore i am constrained to use whatever is provided.
 

Watch MrExcel Video

Forum statistics

Threads
1,118,854
Messages
5,574,673
Members
412,610
Latest member
bluedusty
Top