Home > Back-end >  A batch script to Login to a website by executing keyboard commands
A batch script to Login to a website by executing keyboard commands

Time:02-27

I wrote the script below to login to netflix. It doesn't work (see output below). What am I missing? Any recommendations would be appreciated. Please do explain any solutions that you share.

@if (@CodeSection == @Batch) @then
 /*
REM @echo OFF

set username=MyUserName
set password=MyPassword

if exist "%localappdata%\google\chrome\application\chrome.exe" (
 set browser_exe="%localappdata%\google\chrome\application\chrome.exe"
)  

if exist "%PROGRAMFILES(x86)%\google\chrome\application\chrome.exe" (
 set browser_exe="%PROGRAMFILES(x86)%\google\chrome\application\chrome.exe"
 )

if exist "%PROGRAMFILES%\google\chrome\application\chrome.exe" (
 set browser_exe="%PROGRAMFILES%\google\chrome\application\chrome.exe"
 )
 
    cscript //E:JScript //nologo "%~f0" %*

    set SendKeys=CScript //nologo //E:JScript "%~F0"

    rem : start the browser and navigate to neflix
    %browser_exe% --start-fullscreen --app=https://www.netflix.com/login
    timeout /t 10

    %SendKeys% "{TAB}"
    %SendKeys% "{TAB}"
    %SendKeys% "username"
    %SendKeys% "{TAB}"
    %SendKeys% "password"
    %SendKeys% "{ENTER}"

    exit /b 
@end
*/

// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));

I get this output:

C:\WINDOWS\system32>"C:\Program Files (x86)\google\chrome\application\chrome.exe" --start-fullscreen --app=https://www.netflix.com/login

C:\WINDOWS\system32>timeout /t 10

C:\WINDOWS\system32>CScript //nologo //E:JScript "C:\Test_Script.bat" "{TAB}" C:\Test_Script.bat(59, 3) Microsoft JScript compilation error: Expected '/'

C:\WINDOWS\system32>CScript //nologo //E:JScript "C:\Test_Script.bat" "{TAB}" C:\Test_Script.bat(59, 3) Microsoft JScript compilation error: Expected '/'

C:\WINDOWS\system32>CScript //nologo //E:JScript "C:\Test_Script.bat" "username" C:\Test_Script.bat(59, 3) Microsoft JScript compilation error: Expected '/'

C:\WINDOWS\system32>CScript //nologo //E:JScript "C:\Test_Script.bat" "{TAB}" C:\Test_Script.bat(59, 3) Microsoft JScript compilation error: Expected '/'

C:\WINDOWS\system32>CScript //nologo //E:JScript "C:\Test_Script.bat" "password" C:\Test_Script.bat(59, 3) Microsoft JScript compilation error: Expected '/'

C:\WINDOWS\system32>CScript //nologo //E:JScript "C:\Test_Script.bat" "{ENTER}" C:\Test_Script.bat(59, 3) Microsoft JScript compilation error: Expected '/'

CodePudding user response:

Does this completely untested help you out?

It uses VBScript instead of JScript; just replace the login and password on lines 3 and 4 respectively, and as needed. Do not change anything else whilst testing it.

<!-- :
@Echo Off
Set "[email protected]"
Set "usr_pass=P455w0rd"
Set "usr_chrome="
For %%G In (
    "%ProgramFiles(x86)%"
    "%ProgramFiles%"
    "%LocalAppData%"
) Do If Exist "%%~G\Google\Chrome\Application\chrome.exe" Set "usr_chrome=%%~G\Google\Chrome\Application\chrome.exe"
If Defined usr_chrome %SystemRoot%\System32\cscript.exe //NoLogo "%~f0?.wsf" "%usr_chrome%" "%usr_name%" "%usr_pass%"
Exit /B
-->
<Job>
  <Script Language="VBScript">
    Dim ObjShell, MyChrome
    Set ObjShell = WScript.CreateObject("WScript.Shell")
    MyChrome = Chr(34) & WScript.Arguments(0) & Chr(34)
    ObjShell.Run MyChrome & "--start-fullscreen --app=""https://www.netflix.com/login""", 9
    WScript.Sleep 5000
    ObjShell.SendKeys "{TAB}"
    ObjShell.SendKeys "{TAB}"
    ObjShell.SendKeys(WScript.Arguments(1))
    ObjShell.SendKeys "{TAB}"
    ObjShell.SendKeys(WScript.Arguments(2))
    ObjShell.SendKeys "{ENTER}"
  </Script>
</Job>

There is absolutely no need to be running this script 'As administrator', i.e. elevated, so please do not unnecessarily try to do so.

I have use a wait time of 5 seconds, (5000), to allow your browser to open fully to the page you require before sending the keystrokes. If the login does not work as expected, you may need to increase that time.

  • Related