Как заполнять поля ввода, такие как ввод с клавиатуры в PhantomJS - PullRequest
0 голосов
/ 14 марта 2020

Я новичок в Призраках JS, и мне нужна ваша любезная помощь на случай взрыва

Мне нужно заполнить поля для входа в систему, но мне нравится ввод с клавиатуры, поскольку класс изменяется при наборе текста.

Пожалуйста, проверьте мой код. Мне нужен другой метод вставки при наборе текста. Я слишком много искал, но не нашел.

var testindex = 0;
var loadInProgress = false;//This is set to true when a page is still loading

/*********SETTINGS*********************/
var webPage = require('webpage');
var page = webPage.create();
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36';
page.settings.javascriptEnabled = true;
page.settings.loadImages = false;//Script is much faster with this field set to false
phantom.cookiesEnabled = true;
phantom.javascriptEnabled = true;

/*********SETTINGS END*****************/

console.log('All settings loaded, start with execution');
page.onConsoleMessage = function(msg) {
    console.log(msg);
};
/**********DEFINE STEPS THAT FANTOM SHOULD DO***********************/
steps = [

    //Step 1 - Open Amazon home page
    function(){
        console.log('Step 1 - Open TEData Site');
        page.open("https://my.te.eg/#/home/signin", function(status){

        });
    },
    //Step 2 - Click on the Sign in button
    /*function(){
        console.log('Step 2 - Click on the Sign in button');
        page.evaluate(function(){
            document.getElementById("nav-link-yourAccount").click();
        });
    },*/
    //Step 3 - Populate and submit the login form
    function(){
        console.log('Step 2 - Fill the login form');
        page.evaluate(function(){
            document.forms[0].MobileNumberID.value="user";
            document.forms[0].PasswordID.value="password";
            //document.getElementById("MobileNumberID").click();
            //document.getElementById("MobileNumberID").value='user';
            //document.getElementById("PasswordID").click();
            //document.getElementById("PasswordID").value='password';
            //document.getElementById("singInBtn").disabled = false;
            document.getElementById("singInBtn").click();
        });
    },
    //Step 4 - Wait Amazon to login user. After user is successfully logged in, user is redirected to home page. Content of the home page is saved to AmazonLoggedIn.html. You can find this file where phantomjs.exe file is. You can open this file using Chrome to ensure that you are logged in.
    /*function(){
        console.log("Step 4 - Wait Amazon to login user. After user is successfully logged in, user is redirected to home page. Content of the home page is saved to AmazonLoggedIn.html. You can find this file where phantomjs.exe file is. You can open this file using Chrome to ensure that you are logged in.");
         var fs = require('fs');
         var result = page.evaluate(function() {
            return document.querySelectorAll("html")[0].outerHTML;
        });
        fs.write('AmazonLoggedIn.html',result,'w');
    },*/
];
/**********END STEPS THAT FANTOM SHOULD DO***********************/

//Execute steps one by one
interval = setInterval(executeRequestsStepByStep,50);

function executeRequestsStepByStep(){
    if (loadInProgress == false && typeof steps[testindex] == "function") {
        //console.log("step " + (testindex + 1));
        steps[testindex]();
        testindex++;
    }
    window.setTimeout(function () {
            if (typeof steps[testindex] != "function") {
        console.log("test complete!");
             page.render('image.jpeg', {format: 'jpeg', quality: '100'});
        phantom.exit();
    }
        }, 3000); // Change timeout as required to allow sufficient time 


}

/**
 * These listeners are very important in order to phantom work properly. Using these listeners, we control loadInProgress marker which controls, weather a page is fully loaded.
 * Without this, we will get content of the page, even a page is not fully loaded.
 */
page.onLoadStarted = function() {
    loadInProgress = true;
    console.log('Loading started');
};
page.onLoadFinished = function() {
    loadInProgress = false;
    console.log('Loading finished');

};
page.onConsoleMessage = function(msg) {
    console.log(msg);
};

Я использовал этот код, чтобы изменить ввод, но не обнаружил вставку и классы не влияли

document.forms[0].MobileNumberID.value="user";
                document.forms[0].PasswordID.value="password";
                //document.getElementById("MobileNumberID").click();
                //document.getElementById("MobileNumberID").value='user';
                //document.getElementById("PasswordID").click();
                //document.getElementById("PasswordID").value='password';

Спасибо

...