open a web page with batch/cmd and execute javascript

  • 9 months ago
Comment

ChatGPT
1 / 1

You can use Windows Script Host (WSH) to open a web page and execute JavaScript code using a batch file. Here's how you can do it:

  1. Create a new batch file (e.g., run_js.bat).
  2. Open the batch file in a text editor and add the following lines:
batch
@echo off

:: Define the URL of the web page
set "url=https://www.example.com"

:: Define the JavaScript code to execute
set "javascript=alert('Hello, World!');"

:: Use Windows Script Host to open the web page and execute JavaScript
echo var IE = new ActiveXObject("InternetExplorer.Application") > temp.js
echo IE.Navigate("%url%") >> temp.js
echo IE.Visible = true >> temp.js
echo IE.Document.parentWindow.execScript("%javascript%") >> temp.js
echo WScript.Sleep 5000 >> temp.js
echo IE.Quit >> temp.js
cscript //nologo temp.js
del temp.js
  1. Replace https://www.example.com with the URL of the web page you want to open.
  2. Replace alert('Hello, World!'); with the JavaScript code you want to execute on the web page.
  3. Save the batch file.

When you run the batch file (run_js.bat), it will use Windows Script Host to open the specified web page in Internet Explorer and execute the JavaScript code. The code above waits for 5 seconds before closing the Internet Explorer window. You can adjust the sleep time as needed.

Please note that this method relies on Internet Explorer and may not work with all websites or modern web technologies. It's recommended to use a modern browser and dedicated automation tools like Selenium for more complex web automation tasks.