javascripthtmlgetelementsbyname

getelementsbyname not working on diffrnrt web


Good day to all. I'm new here, so I apologize if I do something wrong.

What I'm trying to do ... I'm trying to create a Bat file that would automatically open a specific site and enter there login and password, nadimal "Login" ... Emulation of what I attended today on this site ... In the past, I did it quite well ... Here is an example. (Login and password changed)

@set @script=0 /*
@echo off
set @script=
@cscript /nologo /e:jscript "%~dpnx0"
@exit /b
*/
 
var url,login,password;
url      = "http://elearn2.mbschool.ru/lmsonline/index.jsp";
// заменить на свои учетные данные
login    = "k";
password = "gx";
 
var IE = WScript.CreateObject("InternetExplorer.Application");
IE.visible = 1;
IE.navigate(url); 
 
while (IE.Busy) {
  WScript.Sleep(200);
}
 
IE.document.getElementsByName("user")(0).value = login;
IE.document.getElementsByName("password")(0).value = password;
button = IE.document.getElementsByName("save")(0);
button.focus();
button.click();

A little time has passed and the developers have made changes to the site ... For the moment, such an algorithm does not work for some reason

@set @script=0 /*
@echo off
set @script=
@cscript /nologo /e:jscript "%~dpnx0"
@exit /b
*/
 
var url,login,password;
url      = "http://lms.mba.ru/mira/#&step=1&s=tT79vdH8nG0XWayfK0Lq&type=login&doaction=Go";
// заменить на свои учетные данные
login    = "k;
password = "6m";
 
var IE = WScript.CreateObject("InternetExplorer.Application");
//IE.visible = 0;
IE.navigate(url); 
IE.visible = 1;
 
while (IE.Busy) {
  WScript.Sleep(4000);
}

IE.document.getElementsByName("user")(0).value= login;
IE.document.getElementsByName("password")(0).value = password;
button = IE.document.getElementsByName("doaction")(0);
button.focus();
button.click();

For some reason, the login and password do not appear in the forms and the button is not pressed ... Help me please. Thank you.


Solution

  • If you had looked at the page's source code, you would have seen that the user and password text boxes are inside of an iframe. To manipulate DOM elements inside of an iframe, you need to load the content first.

    @set @script=0 /*
    @echo off
    set @script=
    @cscript /nologo /e:jscript "%~dpnx0"
    @exit /b
    */
    
    var url,login,password;
    url      = "http://lms.mba.ru/mira/#&step=1&s=tT79vdH8nG0XWayfK0Lq&type=login&doaction=Go";
    
    login    = "k";
    password = "6m";
    
    var IE = WScript.CreateObject("InternetExplorer.Application");
    //IE.visible = 0;
    IE.visible = 1;
    IE.navigate(url); 
    
    while (IE.Busy) {
      WScript.Sleep(4000);
    }
    
    IE.document.getElementsByClassName("mira-frame-content")[0].contentDocument.getElementsByName('user')[0].value= login;
    IE.document.getElementsByClassName("mira-frame-content")[0].contentDocument.getElementsByName('password')[0].value = password;
    button = IE.document.getElementsByClassName("mira-frame-content")[0].contentDocument.getElementsByTagName("button")[1];
    button.focus();
    button.click();