|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
WSH - Accessing DHTML elements from outside
I'm trying to create a script that will wait till Internet Explorer starts. Then it will access its windows and be able to, lets say, get rid of certain pictures that I don't want.
Using WMI, I think I can get the handle of a new process. I don't know if that will help me. Furthermore, if I have the hWnd of an IE window can I access the HTML inside it somehow using the Shell.Application? |
|
#2
|
|||||||||||||||||
|
|||||||||||||||||
|
Let me just say that this is an awesome question although complicated in nature. Luckily, the Windows Guru has a solution just for you.
Let's approach this in two parts:
Creating an event-driven WMI script to watch for a new process: WMI provides a little known facet that allows you to write event-driven scripts that react to the occurrence of a specified event rather than executing linearly as soon as the script is launched. Let's look at how to watch for the creation of an instance of the Iexplore.exe process which indicates that Internet Explorer has been launched. To begin, you'll need to have a basic understanding of how events work in WMI. Your script will create an event consumer. This is basically a listener that waits for notification that a specific event has occurred. WMI will trigger event notifications for a few basic event types. This is beyond the scope of this post so I will only refer to the specific event we'll be using. For more information on WMI event scripting, please read my article series here on ASP Free that explains this in more detail. Event Scripting with WMI More Event Scripting with WMI WMI has an intrinsic event called __InstanceCreationEvent that is triggered whenever a new instance is added to the WMI namespace. As you may well know, WMI's Win32_Process class houses instances of currently running processes. So each time a new process is launched, a new instance of the Win32_Process class is added to WMI's namespace resulting in an __InstanceCreationEvent trigger. By monitoring this event, a script can be notified any time a specific process is started. vb Code:
vb Code:
vb Code:
In the sample code above, I've demonstrated how you can use this WMI event script. Run the code and then open Internet Explorer. As soon as the program starts, WMI fires the event and the script begins to work displaying a message box with some information about the target process. As you can see, you can retrieve the Hwnd (window handle) for the new instance of Internet Explorer as per your question. You probably won't need it though as you'll soon see. Okay, let's put this script to work now. You're going to remove everything inside of the Do loop above and place any functioning code you need in its place. Let's address the second part of your question and then I'll show you how you can put the two together. Capturing instances of Internet Explorer: Ordinarily you would capture a running process using VBScript's GetObject method. However, it's a bit restrictive in that it doesn't allow you to specify between multiple instances of the same application. In fact, it will usually result in an error if you try. Luckily, the Shell Automation Object provides us with an alternative. It's Windows method returns a ShellWindows Object that represents a collection of Explorer Shell windows. Due to Internet Explorer's close integration with the Explorer Shell, instances of Internet Explorer are also treated as Shell Windows. Each object in the ShellWindows Object collection is an Internet Explorer Object whose Document property gets an instance of the active document's automation object. Nicely enough, this is functionally equivalent to the Document object you would access from a script within the browser. WMI triggers an event as soon as the process is created. This doesn't imply that the program is running or even functional yet. It's very important that you monitor the Internet Explorer Object's Busy property and do not attempt to access the object before it is fully initialized. vb Code:
Notice that I've enclose the latter portion of the script in an If statement that checks that the Document object exists. This is a precautionary measure that will prevent your scripts from breaking under certain circumstances. The Document property WILL NOT return an object if Internet Explorer's script securities are set too high! Now that you've seen both parts of this script, let's see what the whole thing would look like together. vb Code:
__________________
Scripting problems? Windows questions? Ask the Windows Guru! Stay up to date with all of my latest content. Follow me on Twitter! Help us help you! Post your exact error message with these easy tips! |
|
#3
|
|||
|
|||
|
Thank you very much. Actually being that there was no response for a long time, I looked all over the place and came up with pretty much that. In fact I used the .ExecNotificationQueryAsync and _OnObjectReady that you had once told me about.
I actually needed to recall my script through the run(cscript...) because I wanted to monitor IE for the _DocumentComplete event and I read that you cannot use Wscript.ConnectObject for IE if it wasn't loaded with the page. So I started a new script and created IE from fresh and copied the Top, Left, Height, Width, FullScreen, and of course documentUrl from the old IE into the new one. One thing still needs clarafication however: In case I wanted to double check that the window I'm dealing with is the same as the process just begun, I would want to check TargetInstance.Handle to match IE.HWND. The thing is that actually no property returned by WMI matches the IE.HWND. What is going on? |
|
#4
|
||||
|
||||
|
Quote:
Quote:
|
|
#5
|
|||
|
|||
|
How are you guys running this code?
I tried it in both a .VBS file and a .WSF file and I kept getting the following error message: Code:
Line: 34 Error: Object doesn't support this property or method: 'objIE.ReadyState' Code: 800A01B6 Source: Microsoft VBScript runtime error I am running IE6 on Windows 2000 with .NET 2.0. |
|
#6
|
||||
|
||||
|
The code was tested on Windows XP. It's possible that it may not be fully supported on Windows 2000 although I wasn't able to find that in the documentation. See if some error handling can get you around that portion of the code.
|
![]() |
| Viewing: ASP Free Forums > System Administration > Windows Scripting > WSH - Accessing DHTML elements from outside |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|