Can HTAs be used to automate web browsing?

2019-08-30 07:12发布

I am new to HTAs. I just read https://msdn.microsoft.com/en-us/library/ms536496%28v=vs.85%29.aspx and am a bit confused.

Can I use HTAs to automate browsing? Say I want to download a web page and fill in a form automatically, i.e. from a script. How would an HTA help me do this, if at all? It's important that the JavaScript code in the downloaded page is run as usual. I should be able to enter somehow and fill in the form after it has finished initializing, just as if I were a human agent.

1条回答
劳资没心,怎么记你
2楼-- · 2019-08-30 08:09

First, you need to open an IE window, as follows:

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

Then navigate the IE window to the webpage you want:

IE.Navigate("www.example.com");

Wether your IE window is visible or invisible, it's up to you. Use Visible property to make it visible:

IE.Visible = true;

Then, you should wait until the webpage is completely loaded and then run a function that takes your desired actions. To do so, first, get the HTML document object from the webpage using Document property of IE object, then repeatedly check the readyState property of document object. In the code below, it is assumed that you have a function named myFunc, which takes your desired actions on the webpage. (For example, modifying the contents of the webpage.)

var doc = IE.Document;
interval = setInterval(function() {
    try
    {
        if (doc.readyState == "complete")
        {
            myFunc();
            clearInterval(interval);
        }
    }
    catch (e) {}
}, 1000);

In the function myFunc, you can do anything you want with the webpage since you have HTML document object stored in doc variable. You can also use parentWindow property to get the HTML window object.

查看更多
登录 后发表回答