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?

Dylan Valade
  • 5,565
  • 6
  • 42
  • 56
user3416803
  • 379
  • 1
  • 2
  • 12
  • `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. – user3416803 Feb 13 '15 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. – user3416803 Feb 13 '15 at 16:06
  • 1
    Possible duplicate of [How to submit a form using PhantomJS](http://stackoverflow.com/questions/9246438/how-to-submit-a-form-using-phantomjs) – That Brazilian Guy Jun 30 '16 at 14:24

1 Answers1

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
});
Community
  • 1
  • 1
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • Its strange, but as i see the simplest method is document.forms[0].submit(); but it doesn't work. – user3416803 Feb 13 '15 at 16:23
  • No, i'am using 2.0.0, seems to be the last one. Maybe the problem is elsewhere? – user3416803 Feb 13 '15 at 16:26
  • Please register to the [`onConsoleMessage`](http://phantomjs.org/api/webpage/handler/on-console-message.html), [`onError`](http://phantomjs.org/api/webpage/handler/on-error.html), [`onResourceError`](http://phantomjs.org/api/webpage/handler/on-resource-error.html), [`onResourceTimeout`](http://phantomjs.org/api/webpage/handler/on-resource-timeout.html) events. Maybe there are errors. – Artjom B. Feb 13 '15 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 =\ – user3416803 Feb 13 '15 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. – user3416803 Feb 13 '15 at 17:20
  • You have to register the error events before you do anything. Even before `page.open()`. – Artjom B. Feb 13 '15 at 17:28
  • I see. I add page.onError() and all other methods before opening the page, but it showed no errors. I tried to do make some JS erros on target hmtl page(with form) and it revealed all the errors. As for my phantomjs code it showd nothing, like there were no errors. – user3416803 Feb 13 '15 at 17:49
  • I added sample scripts – Artjom B. Feb 13 '15 at 17:56
  • Thanks you a lot! It works fine! Thank you for all help once again! – user3416803 Feb 13 '15 at 18:36