0

I am attempting to use a search box on a website to find a client. I can input information to Internet Explorer with the code under my "Log on to website" portion, but when I attempt to search for the client, the objects I try always are null.

VBA Code

Sub main()

' IE object to pass to all functions
Dim IE As Object
' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")

' URL for website login
Dim URL As String
URL = "https://www.website.com"

' Search bar object (for homepage)
Dim searchBar As Object

IE.Visible = True
IE.navigate URL


' Load webpage completely
Do Until Not IE.busy And IE.readystate = 4
    DoEvents
Loop

' Log on to website
IE.document.getElementByID("UserName").Value = "myUsername"
IE.document.getElementByID("Password").Value = "myPassword"
' Time delay ensures entry of username and password
Application.Wait (Now + TimeValue("00:00:02"))
IE.document.getElementByID("btnLogin").Click

' Load webpage completely
Application.Wait (Now + TimeValue("00:00:02"))
Do Until Not IE.busy And IE.readystate = 4
    DoEvents
Loop

' Search for client by ID on "Home" tab
' This doesn't work
IE.document.all("form1").Name("Searchfor").Value = "clientID"
IE.document.all("form1").Name("search").Click
' This doesn't work
IE.document.getElementsByName("Searchfor")(0).Value = "clientID"
IE.document.getElementsByName("search")(0).Click

End Sub

HTML Code (I tried to put spaces here to make it more readable, but I don't know HTML so it may be split up incorrectly)

<form name="form1" id="form1" action="/common/my_cw.asp" method="post">
<table width="255" border="0" cellspacing="0" cellpadding="2"><tbody>
<tr><td nowrap="">

<input name="Searchfor" onclick="if (this.value == 'NAME / ID') this.value=''" 
onblur="if (this.value == '') 
this.value='NAME / ID'" type="text" size="20" value="NAME / ID">&nbsp;&nbsp;

<input name="search" type="submit" value="Search"></td></tr>

<tr><td>
<input name="searchtype" type="radio" checked="" value="C">Client&nbsp;&nbsp;
<input name="searchtype" type="radio" value="E">Employee</td></tr>
</tbody>
</table>
</form>

I have also tried getElementsByTag("input") and using a for each loop with no success.

Please let me know if additional HTML code is needed.

braX
  • 11,506
  • 5
  • 20
  • 33
  • Im not sure how IE works, but Im sure you have to switch to a different frame in order to select the ID that you want. If you download SideeX Recorder (Google Chrome App) it will show you what frame you are currently in. I had to switch multiple frames to automate my companies ERP – Ethan Jan 30 '20 at 17:30
  • When you click the login button, whether it will open a new tab or a new window? Or, just redirect to another page in the same tab? As far as I know, we could find the elements after redirect different page in the same tab. Besides, please try to use F12 developer tools to check the Html elements, make sure the name value not changed. And, please check your code whether you using Iframe tag or not. – Zhi Lv Jan 31 '20 at 03:35
  • After logging in, IE stays in the same tab. It is going to a new page under the same domain. The HTML code I posted is after the redirect from logging in. There is an iFrame on the page I am trying to search from, but it is not above the elements I am trying to access in hierarchy. The iFrame is under a frame name "banner" and the elements I am trying to access are under a frame named "main". – jtomkiewicz Jan 31 '20 at 14:21
  • As far as I know, if we want to find the elements in the Iframe tag, first, we need to find the Iframe element first, then access the element in the Iframe tag, you could check [this thread](https://stackoverflow.com/questions/57941535/). code as below: `IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("btncontentSayHello").Click` – Zhi Lv Feb 03 '20 at 04:08

0 Answers0