How to get updated page content after JS event using phantomjs?

I have a page with a simple form. This form has ONE input text.

I have this code:

page.open("http://www.example.com", function (status) { page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js", function() { page.evaluate(function() { $('input[name=myText]').val('my content'); }); }); }); 

(as you can see, I also downloaded jquery)

Now, when I set the contents of this input text, the page changes, the contents of the page change.

My question is: how can I get updated content? The problem is that I need to submit the form, but I cannot do it after:

 $('input[name=myText]').val('my content'); 

because when I installed it, the JS event on the page changes, which changes the content.

SO I have to read the new content, find the form using jQuery and then submit it.

Can someone help me?

Thanks!

+4
source share
1 answer

So your call to page.evaluate() has a side effect and this changes the page. The question is whether this change occurs instantly. But you cannot assume that he is doing this - especially if he causes a selection of new content from a remote web server.

At best, you can try to get some sleep, and then assume that your page has new content. Or, alternatively, when you feel more advanced, regularly conduct a survey on the DOM that you expect to see. But the beginner should try to sleep:

 .... page.evaluate( function() { ... } ); window.setTimeout( function() { /* press submit button */ }, 5000 /* wait 5 seconds (5,000ms) */ ); .... 
+2
source

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


All Articles