求助:用VBScript实现登录后修改URL并抓取网页文本至带时间戳文件
Solution Using IE COM Automation (No Extra Tools Needed)
Got it, let's tackle this step by step. Since relying on SendKeys can be super flaky (thanks to window focus issues), we'll use Internet Explorer's COM object model to directly control the browser—this way is way more reliable and fits your rule of not installing extra tools. Here's a complete script that wraps up all your needs:
Full VBScript Code
' Configuration - update these with your actual details strUsername = "your_real_username" strPassword = "your_real_password" strLoginURL = "https://client/comp/acct/Userlogin" strHealthURL = "https://client/comp/acct/Health" strSaveFolder = "E:\Test_Health_Monitor\" ' Step 1: Create timestamped filename (24-hour format example: 24_5_14.txt for 24th May, 14:00) dtCurrent = Now() strDay = Day(dtCurrent) strMonth = Month(dtCurrent) strHour = Hour(dtCurrent) strFileName = strDay & "_" & strMonth & "_" & strHour & ".txt" strFullFilePath = strSaveFolder & strFileName ' Step 2: Launch and control Internet Explorer Set objIE = CreateObject("InternetExplorer.Application") objIE.Visible = True ' Set to False if you want the browser to run in the background objIE.Navigate strLoginURL ' Wait for login page to fully load Do While objIE.Busy Or objIE.ReadyState <> 4 WScript.Sleep 500 Loop ' Step 3: Fill in login credentials and submit ' Replace these IDs with the actual ones from your login page (use F12 in IE to inspect elements) objIE.Document.GetElementById("username_input_id").Value = strUsername objIE.Document.GetElementById("password_input_id").Value = strPassword ' Submit the form (use button click instead if your login uses a separate button) objIE.Document.Forms(0).Submit ' Alternative for button: objIE.Document.GetElementById("login_button_id").Click() ' Wait for post-login redirect to finish Do While objIE.Busy Or objIE.ReadyState <> 4 WScript.Sleep 500 Loop ' Step 4: Navigate to the Health page objIE.Navigate strHealthURL ' Wait for Health page to load completely Do While objIE.Busy Or objIE.ReadyState <> 4 WScript.Sleep 500 Loop ' Step 5: Extract pure text from the page strPageContent = objIE.Document.Body.InnerText ' Step 6: Save text to the timestamped file Set objFSO = CreateObject("Scripting.FileSystemObject") ' Create the folder if it doesn't exist yet If Not objFSO.FolderExists(strSaveFolder) Then objFSO.CreateFolder strSaveFolder End If Set objFile = objFSO.CreateTextFile(strFullFilePath, True) ' True = overwrite existing file objFile.Write strPageContent objFile.Close ' Clean up resources objIE.Quit Set objIE = Nothing Set objFSO = Nothing Set objFile = Nothing WScript.Echo "Done! Data saved to: " & strFullFilePath
Key Adjustments You Need to Make
- Form Field IDs: You'll need to replace
"username_input_id"and"password_input_id"with the actualidornameattributes of your login form's inputs. To find these:- Open the login page in IE.
- Press F12 to open Developer Tools.
- Use the "Select Element" tool (arrow icon) to click the username/password box, then check the HTML for
id="..."orname="...".
- Login Submission: If your login uses a clickable button instead of a form submit, swap out
objIE.Document.Forms(0).SubmitwithobjIE.Document.GetElementById("login_button_id").Click()(use the button's actual ID). - Timestamp Format: Tweak the
strFileNameline if you prefer a different format. For example, to get24May_09AM.txt, use:strFileName = Day(dtCurrent) & Left(MonthName(Month(dtCurrent), True), 3) & "_" & _ IIf(Hour(dtCurrent) < 10, "0" & Hour(dtCurrent), Hour(dtCurrent)) & _ IIf(Hour(dtCurrent) < 12, "AM", "PM") & ".txt" - Wait Timings: If your pages load slower, increase the
WScript.Sleep 500value (e.g.,1000for 1-second waits between checks).
Why This Beats SendKeys
- No window focus issues: The script controls IE directly, so you can use your computer while it runs.
- Reliable page load handling: The
Do Whileloops ensure we never try to interact with a page before it's fully loaded. - Easier to maintain: You won't have to tweak random sleep times every time the site loads a bit slower.
内容的提问来源于stack exchange,提问作者Arty155
相关产品推荐
相关产品推荐

