6

I'm getting familiar with PhantomJS. But I can't get one thing. I have a page with a simple form:

<FORM action="save.php" enctype="multipart/form-data" method="GET" onSubmit="return doSubmit();">
    <INPUT name="test_data" type="text">
    <INPUT name="Submit" type="submit" value="Submit">
</FORM>

and a save.php just writes down the test_data value

so I'm doing this:

page.evaluate(function() {
    document.forms[0].test_data.value="555";
    doSubmit();
});

When rendering the page I see that text field is 555, but form isn't submitting and save.php didn't write down a test_data value. So doSubmit() is not executing, is it? doSubmit() is a simple validation step and a submit is supposed to load the next page.

So the question is: how can I execute a javascript code on the page, using PhantomJS?

3
  • function doSubmit() { if(document.forms[0].test_data.value == "") { alert("Fill the field!"); return false; } return true; } Here it is, sorry i forgot to post it in my question. It's just doing the validation. Commented Feb 13, 2015 at 16:01
  • Oh, i see now. So i just need to click the submit button via JS? Anyway. If i need to execute the function, will it work this way or there is another approach? Sorry for being lame. Commented Feb 13, 2015 at 16:06
  • 1
    Possible duplicate of How to submit a form using PhantomJS Commented Jun 30, 2016 at 14:24

1 Answer 1

9

It seems that you want to submit the form. You can achieve that in different ways, like

After that you will have to wait until the next page is loaded. This is best done by registering page.onLoadFinished (which then contains your remaining script) right before submitting the form.

page.open(url, function(){
    page.onLoadFinished = function(){
        page.render("nextPage.png");
        phantom.exit();
    };
    page.evaluate(function() {
        document.forms[0].test_data.value="555";
        document.forms[0].submit();
    });
});

or you can simply wait:

page.open(url, function(){
    page.evaluate(function() {
        document.forms[0].test_data.value="555";
        document.forms[0].submit();
    });
    setTimeout(function(){
        page.render("nextPage.png");
        phantom.exit();
    }, 5000); // 5 seconds
});
9
  • Its strange, but as i see the simplest method is document.forms[0].submit(); but it doesn't work. Commented Feb 13, 2015 at 16:23
  • No, i'am using 2.0.0, seems to be the last one. Maybe the problem is elsewhere? Commented Feb 13, 2015 at 16:26
  • Please register to the onConsoleMessage, onError, onResourceError, onResourceTimeout events. Maybe there are errors.
    – Artjom B.
    Commented Feb 13, 2015 at 16:32
  • Sorry, i try to add those functions to the code, but they didn't work or phantom stucked. I just copypasted examples from documentation and put it after page.evaluate(). Nothing worked =\ Commented Feb 13, 2015 at 16:57
  • Strange, i used couple diffrent ways, including clicking but no one didn't work. How could so simple task create so many troubles. Commented Feb 13, 2015 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.