Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

internet explorer - VBScript - How do I create a new sub that focuses on an existing IE window WITHOUT creating a new object?

I am trying to create an automation tool for users on a web-based system that have to login. I am not able to use CreateObject because that opens a new IE window which makes the user sign-in again which then kicks them out of the current session.

How do I rewrite the following so it targets the existing IE window and clicks on a button without using CreateObject?

Sub exampleOne
    Dim ie, doc
    set ie = 'cannot use CreateObject - what else can I do? 
    set doc = ie.document
    wshell.AppActivate("Sample Title")      
    doc.getElementByID("button").Click
End Sub
question from:https://stackoverflow.com/questions/65927486/vbscript-how-do-i-create-a-new-sub-that-focuses-on-an-existing-ie-window-witho

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I suggest you try to refer to the example below may help you to focus existing IE window and click the button on a webpage.

Sample VBScript:

Set shell = WScript.CreateObject("WScript.shell")

Dim objInstances, objIE, IE
Set objInstances = WScript.CreateObject("Shell.Application").windows
If objInstances.Count > 0 Then 
    For Each objIE In objInstances
        If InStr(objIE.LocationURL,"https://Your_site_URL_here...") > 0 then  '/// Modify the URL on this line...
        Set IE = objIE
        End if
Next
End If
shell.AppActivate ("Internet Explorer")

Wait IE, 2000
IE.document.getElementById("btn1").Click

Wait IE, 2000

'IE.Quit

Sub Wait(IE, ms)
    Do
        WScript.Sleep ms
    Loop While IE.ReadyState < 4 And IE.Busy
End Sub

Further, you can try to understand the code example try to modify it based on your own requirements.

Thanks for your understanding.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...