使用 ShellExecute 正确识别打开的 Web 浏览器以使用 document.getElement

Identify an opend Webbrowser with ShellExecute properly to work with document.getElement

我需要通过js在网页中填写表格来自动化我的工作,我做了这样的代码:

var ie = new ActiveXObject("InternetExplorer.Application");

ie.visible = true;

ie.navigate("http://www.google.com");

while (ie.busy) WScript.Sleep(100);


ie.document.getElementsByName('q')[0].value ="3";


var Butn = ie.document.getElementsByName('btnK')[0];

 Butn.click();

但我希望这段代码能与另一个网络浏览器 (e.g.Firefox) 一起使用,所以我尝试了这样的操作:

var ie = new ActiveXObject("Shell.Application");
 
var commandtoRun ="C:\Program Files\Mozilla Firefox\firefox.exe";

ie.ShellExecute(commandtoRun,"http://www.google.com", "1");


while (ie.busy) WScript.Sleep(100);


 ie.document.getElementsByName('q')[0].value ="3";


var Butn = ie.document.getElementsByName('btnK')[0];

 Butn.click();

但是这段代码抛出错误:

'ie.document' 为空或不是对象

800A13F

我要求以下两种方式中的任何一种:

*处理已经打开的网络浏览器,而不是只打开一个然后应用代码。

*以正确的方式识别打开的网络浏览器以应用代码。

我看到您使用的是仅 IE 浏览器支持的 ActiveX 对象。

此对象是 Microsoft 扩展,仅在 Internet Explorer 中受支持。

如果您正在寻找自动化其他浏览器(如 Firefox),我建议尝试检查 Selenium web driver。我认为它是更适合满足您要求的产品。

可以使用多种开发语言来自动化浏览器。

貌似也可以得到已经打开的浏览器对象

看这里:Can we use Selenium to work with an already open browser session?