Batch file that changes URL in open browser

2020-03-07 06:25发布

问题:

I've seen something like this:

start /d "C:\Program Files\Internet Explorer (x86)\IEXPLORE.EXE" www.google.com

But this just opens a new tab. I would like it to change the whatever website is open to the one listed.

The end goal is to create a batch file that switches between two webpages with a given delay between them.

回答1:

You should be able to do this using SendKeys, see here.

Basically, you look at the dropdown menus of Internet Explorer (i.e. File, Edit etc) and look at the shortcut keys for what you would want to do, then you write a little bit of VBscript to send those keystrokes. You save the VBscript in a file with the ".VBS" extension and then you can just double-click it to run it. Something like this should get you started....

 set WshShell = WScript.CreateObject("WScript.Shell")
 WshShell.Run "iexplore"
 WScript.Sleep 500
 WshShell.AppActivate "Windows Internet Explorer"
 WshShell.SendKeys "www.google.com"

If you have already started IE using your own START command, you can just send the keystrokes you need to control it after that and you will not need the WshShell.Run part I have.



回答2:

You may use SendKeys directly in your Batch file, as shown in this or this or this answer; for example:

@if (@CodeSection == @Batch) @then

@echo off

rem Start default IE
start /d "C:\Program Files\Internet Explorer (x86)\IEXPLORE.EXE" www.google.com

:changeLoop
CScript //nologo //E:JScript "%~F0" "keys to change to first webpage"
timeout /T 10
CScript //nologo //E:JScript "%~F0" "keys to change to second webpage"
timeout /T 10
goto changeLoop

@end

WScript.CreateObject("WScript.Shell").SendKeys(WScript.Arguments(0));