首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

自動化網頁表單填寫:以 Gmail 登入為例(JScript + WSH)

     編輯:關於C語言

技術架構:Windows Script Host + Internet Explorer 自動化

此技術使用 JScript 搭配 WSH (Windows Script Host),透過 InternetExplorer.Application COM 物件自動化 IE 瀏覽器,實現網頁表單自動填寫與提交。注意:這不是 C# 程式碼,而是獨立的 .js 腳本。

核心腳本:googleLogin.js

var ie = WScript.CreateObject("InternetExplorer.Application");
var args = WScript.Arguments;
var targetUrl = "https://www.google.com/";

if (args.length > 0)
    targetUrl += args(0);

ie.Navigate("https://www.google.com/accounts/Login?continue=" + targetUrl);
WaitForIE(ie);

var doc = ie.Document;
doc.forms[0].Email.value = "your_account@gmail.com";
doc.forms[0].Passwd.value = "你的密碼";

// 處理「保持登入」核取方塊可能不存在的情況
if (doc.forms[0].PersistentCookie != null)
    doc.forms[0].PersistentCookie.checked = false;

doc.forms[0].submit();
WaitForIE(ie);

ie.Visible = true; // 最後才顯示視窗

function WaitForIE(ieObj)
{
    while (ieObj.Busy)
        WScript.Sleep(100);
}

批次檔包裝:google.bat

@echo off
wscript "%windir%\system32\googleLogin.js" %1 %2 %3 %4 %5 %6 %7 %8 %9

部署與使用

  1. 將 googleLogin.js 與 google.bat 放入 %windir%\system32\(或任何在 PATH 環境變數的目錄)
  2. 命令列執行:google mail → 直接登入 Gmail
  3. 支援參數:google notebook(筆記本)、google bookmarks(書籤)等
  4. 無參數執行 google → 以登入身分進入 Google 首頁

安全性強化建議

方法說明安全性
明文密碼腳本中直接寫死密碼❌ 極低,任何人可讀取
screnc.exe 加密產生 .jse 加密腳本⚠️ 低,scrdec 可解密
環境變數讀取WScript.CreateObject("WScript.Shell").Environment("USER")("GMAIL_PWD")✅ 中,密碼不在檔案中
Windows Credential Manager呼叫 CredRead API 讀取儲存的憑證✅ 高,系統級加密儲存
改用 C# + Selenium/WebDriver現代化瀏覽器自動化框架✅ 最高,支援無頭模式、多瀏覽器

現代化替代方案:C# + Selenium

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

var options = new ChromeOptions();
options.AddArgument("--headless"); // 無頭模式
using (var driver = new ChromeDriver(options))
{
    driver.Navigate().GoToUrl("https://accounts.google.com/signin");
    driver.FindElement(By.Id("identifierId")).SendKeys("your@gmail.com");
    driver.FindElement(By.Id("identifierNext")).Click();
    // 等待密碼欄位出現...
    driver.FindElement(By.Name("password")).SendKeys("password");
    driver.FindElement(By.Id("passwordNext")).Click();
    // 登入完成
}

總結

  • JScript + WSH + IE 自動化屬於舊世代技術,僅適用於舊版 IE 環境
  • 現代開發建議使用 Selenium WebDriver、Playwright 或 Puppeteer Sharp
  • 絕對不要在原始碼中硬編碼密碼,應使用憑證管理器或安全儲存
  • Google 現代登入流程包含雙因子驗證、JavaScript 動態渲染,舊版 IE 自動化可能失效
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved