How to submit a form using PhantomJS?

I am familiar with PhantomJS. But I can’t get one. 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 save.php just writes the value of test_data p>

so i do this:

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

When rendering the page, I see that the text field has a value of 555, but the form is not submitted, and save.php does not record the value of test_data. So doSubmit() not executing, right? doSubmit() is a simple validation step, and it is assumed that the sender will load the next page.

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

+6
source share
1 answer

It seems that you want to submit the form. You can achieve this in many ways, for example

After that, you have to wait until the next page loads. This is best done by registering page.onLoadFinished (which then contains the remaining script) 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 just 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
source

Source: https://habr.com/ru/post/982437/


All Articles