使用PhantomJS

时间:2018-04-04 01:55:07

标签: javascript forms phantomjs submit

真正的初学者问题:我正在尝试使用PhantomJS提交使用GET方法的表单。我一直试图在

上使用这种技术(已修改)
  

How to submit a form using PhantomJS

但没有成功。有问题的网页上的代码是:



<div id="loginHeader">
	<form name="loginForm" id="loginForm" method="get">
		<input id="email" type="text" data-i18n="[placeholder]common.email" placeholder="E-mail">
		<input id="password" type="password" data-i18n="[placeholder]common.password" placeholder="Password">
		<button class="btn" type="submit" id="loginSubmit" autofocus="" data-i18n="login.login">LOGIN</button>
		<br>
		<a id="forgotPassword" href="#" data-i18n="login.forgot_password">Forgot your password?</a>
	</form>
	<button id="registerButton" class="btn btn-medium" data-i18n="login.register">Register</button>
</div>
&#13;
&#13;
&#13;

我尝试了document.getElementById方法,因为每个元素都有一个ID,而不是前面帖子中的ClassName,但我想知道该代码是否只适用于POST方法(删除&# 34;如果&#34;位,不用说)。

可能我不知道如何触发提交按钮

我非常感激,因为我在黑暗中挣扎。

(为回应第一条评论而添加......)

这是我一直在使用的代码(起点是另一篇文章中的解决方案代码)。

var page = new WebPage(), testindex = 0, loadInProgress = false;

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

page.onLoadStarted = function() {
  loadInProgress = true;
  console.log("load started");
};

page.onLoadFinished = function() {
  loadInProgress = false;
  console.log("load finished");
};

var steps = [
  function() {
    //Load Login Page
    page.open("http://www.abcdefghi.com/");
  },
  // this next bit was my attempt...
    function() {
        //Enter Credentials
        page.evaluate(function() {
            var usr = document.getElementById("email");
            var pswd = document.getElementById("password");
            usr.value = "xxxxx";
            pswd.value = "xxxxx";

      });
   },

    function() {
        //submit
        page.evaluate(function() {
            var button = document.getElementById("loginSubmit");
            button.click();
      });
   },
// end of my attempt....
  function() {
    // Output content of page to stdout after form has been submitted
    page.evaluate(function() {
      console.log(document.querySelectorAll('html')[0].outerHTML);
    });
  }
];

interval = setInterval(function() {
  if (!loadInProgress && typeof steps[testindex] == "function") {
    console.log("step " + (testindex + 1));
    steps[testindex]();
    testindex++;
  }
  if (typeof steps[testindex] != "function") {
    console.log("test complete!");
    phantom.exit();
  }
}, 50);

最终结果是原始网页HTML / js

的console.log输出

0 个答案:

没有答案