Batch file to switch between browser tabs - windows

I have two browser tabs running monitoring applications on a screen. I want to automate switching between tabs after lets say every 2 mins. I think this can be achieved by creating a scheduled task in windows which execute a batch file every 2 mins. Batch file will switch the tab. I need help creating such batch file. Thanks.

You can use VBScript to do that,
Example for IE :
Create a file Switch.vbs with this :
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "Internet Explorer"
Wscript.Sleep 1500
WshShell.SendKeys "^{TAB}"
Wscript.Sleep 1500
After opening your 2 tabs in IE run the command every 2 minutes :
wscript.exe switch.vbs

Related

Create a VB script to send the keys to open up a program with specific settings

I am trying to create a VB script to send the keys to open up a program/application with specific settings. So, after the application has been started, choosing those very specific settings would involve pressing 2 buttons inside the application UI (1st button to choose specific set of options, and 2nd button to save those options).
I will then create a batch file to call the mentioned VB script on boot.
So far, I got this:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "^%1"
I've searched and searched, but couldn't figure out how to be able choose/save options once the app has been started, if it's even possible?
Thank you in advance for helping out!
Disclaimer: I am not a technical person, so forgive me if noob question :)
Here's a script to start an application (notepad), then send some keystrokes to it:
'VBScript Example
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "c:\windows\notepad.exe"
' add delay here
WshShell.AppActivate "Notepad"
WshShell.SendKeys "Hello World!"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "abc"
WshShell.SendKeys "{CAPSLOCK}"
WshShell.SendKeys "def"
Perhaps this can be adapted to your needs?
The WshShell.AppActivate command is used to bring the program (with the specified window title) to the foreground.
You might want to add a delay to allow the program time to start before sending the keystrokes. This can be done by adding a sleep() call just after Wshell.Run:
' Sleep for 5 seconds (5000 msec)
WScript.Sleep(5000)
Also, here's a list of key-codes that you can use.

WScript.Shell SendKeys not working on Windows 8.1 x64

I'm having problems with the following VBScript.
I'm trying to send keystrokes to a web browser window, but nothing happens.
The first two lines in the following script works fine, but the third line with the SendKeys command, doesn't do anything.
Set objShell = CreateObject("WScript.Shell")
objShell.Run "http://someurl.com", 1
objShell.SendKeys "Some Text"
I've tried to run the script on two different Win 8.1 machines. On Win 8.1 Pro it works, but on Win 8.1 not-Pro it won't work. Is there any setting somewhere in Windows that needs to be set or anything other that can help me?
Update
New code tried (added sleep and running browser explicit), but still no luck
Set ObjShell = WScript.CreateObject("WScript.Shell")
objShell.Run """C:\Program Files\Internet Explorer\iexplore.exe""http://someurl.com/"
WScript.Sleep 5000
objShell.SendKeys "Some Text"
While its admittedly not a great programming technique, place a Sleep command after your objShell.Run line (as shown below) to give the OS some time to "catch up" and actually run the URL, activate the browser window to give it focus (you have to take note of the title in the Browser window and use that title exactly), because I have seen the browser window lose focus in some cases and then text cannot be piped into it, and finally it sends the text. Your mileage may vary, and you might even have to increase the sleep time in order to be successful. I've had to sleep up to 30 seconds, in some cases. Especially where, for whatever reason, the web page was slow to render - due to network latency, slow OS, etc.. I haven't faced this problem on Windows 8.1 because I haven't tried it on that platform. I don't think this has anything to do with the OS version; I have faced this same problem with SendKeys on other Windows OS versions where I've had to use the Sleep function like this. The SendKeys method has known limitations, and as it runs outside of the browser you have to wait for certain actions to complete before you can have it do more actions.
Set ObjShell = WScript.CreateObject("WScript.Shell")
objShell.Run "http://someurl.com", 1
WScript.Sleep 20000
objShell.appactivate("Welcome to SOMEURL.COM! - Mozilla Firefox")
WScript.Sleep 700
objShell.SendKeys "Some Text"
Also, you might try replacing line 2 in the above to the following, to see if you have better luck. In the example below, Firefox is the default web browser. Change the path accordingly to the browser of your choice:
objShell.Run("""C:\Program Files\Mozilla Firefox\firefox.exe""http://someurl.com")

How to click on ok on remote machine while starting machine by using vbscript

I am connecting to Remote machine successfully by using below VBScript But the problem is it is stopping at OK button. If once I click on ok Manually then machine is opening.
Set WshShell = WScript.CreateObject("WScript.Shell")
Set cloner = CreateObject("WScript.Shell")
cloner.run"mstsc"
'run remote desktop connection
WScript.Sleep 1000
cloner.SendKeys"DV01234"
cloner.SendKeys("{Enter}")
WScript.Sleep 1000
cloner.SendKeys"password"
cloner.SendKeys("{Enter}")
WScript.Sleep 1000
'cloner.run"cmd"
CAN ANY ONE HELP ME HOW TO CLICK OK BY USING VB SCRIPT ?
If you are using UFT 11.50 (UFT is the new name for QTP) you can use Insight in order to use image based identification to identify the OK button and click on it.

refresh firefox from .bat file/ command

I am attempting to refresh Firefox whenever a bat file is called. I Dont want an extension. I need to know if there is a command that can be used from within a .bat file to reload the current browser or tab.
I know you can use
start firefox as a command but this simply opens a new firefox instance. I need to refresh the current instance.
I was just messing around and came up with this half-working solution that might help a little. Create a file called something like refresh.vbs and paste this in:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate("Google Chrome")
WScript.Sleep 500
WshShell.SendKeys "{TAB}"
WScript.Sleep 100
WshShell.SendKeys "{F5}"
I could only test it with Chrome. The script activates Chrome, sends a tab, and then sends F5 to refresh. It works when I have Chrome up showing one web page, but not when there are multiple web tabs open (because AppActivate activates the Window, but does not give focus to anything).
Maybe it works better with Firefox. There's probably some way to enumerate the tabs in a browser and activate it in vbs, but I don't know it.
If you spawn the browser within the vbs (see WshShell.Run, and the example in the SendKeys documentation), you can get the process number and send messages directly to that Window instead of relying on the app title.
You can invoke the .vbs from within a batch file if you need to:
#echo off
refresh.vbs
On Error Resume Next
Set objExplorer = CreateObject("InternetExplorer.Application")
objExplorer.Navigate "http://www.microsoft.com/technet/scriptcenter"
objExplorer.Visible = 1
Wscript.Sleep 5000
Set objDoc = objExplorer.Document
Do While True
Wscript.Sleep 30000
objDoc.Location.Reload(True)
If Err <> 0 Then
Wscript.Quit
End If
Loop

How to set focus on file dialog opened in Vbscript

Our Team is automating tests/test data preparation in QTP and we do the scripting in VBScript.
In several tests the tester who runs the script need to supply an MS-Excel file with the indata. We use UserAccounts.CommonDialog for this and it works great. Except for one litle problem, when we run this from QTP the file dialog does not get focus. It's opened in the background and it's easy for the tester that runs the script to miss it and waste time waiting for the dialog.
How do we give the file dialog focus?
Code Example:
Set ObjFSO = CreateObject("UserAccounts.CommonDialog")
ObjFSO.Filter = "MS-Excel Spreadsheets|*.xls|All Files|*.*"
while ObjFSO.ShowOpen = false
msgbox "Script Error: Please select a file!"
wend
msgbox "You selected the file: " & ObjFSO.FileName
My guess is that since the dialog is modal, the ShowOpen method doesn't return the execution control back to the script until the dialog is closed. So there's no way to interact with the dialog as part of your test script.
As a workaround, you could spawn a parallel script that would wait for the dialog and activate it. But I guess QTP cannot run two scripts in parallel, so you'll probably need an external shell script (written in VBScript / JScript / PowerShell / etc).
Edit: Try the following:
Create an external VBScript file (.vbs) with the following contents:
Set oShell = CreateObject("WScript.Shell")
While Not oShell.AppActivate("Open")
WScript.Sleep 500
Wend
This script calls WshShell.AppActivate to activate a window whose title contains Open (replace it with the actual dialog title). If there's no such widnow at the monent, it retries the attempt after 0.5 sec (you can increase the delay if you wish).
Launch this script from your QTP test before opening the dialog. (Use SystemUtil.Run or something like this.)
I'm not sure, but I think this should do the trick.
Did you try recording a click on the dialog - so that QTP will click on it to set focus before proceeding?

Resources