A
A
Alexander Knyazev2017-01-20 11:00:59
JavaScript
Alexander Knyazev, 2017-01-20 11:00:59

How to embed asynchronous code in a page using PhantomJs?

Using page.open() I open the page at the address, then I need to embed the code that:
1) will click on the button.
2) Wait until the new DOM elements of the site are loaded (well, or at least until the specified number of seconds pass)
3) Then work with these elements.
I embed the code on the page using page.evaluate
Here is my sample code:

page.open(url, function(status) {
  
  var elements_array = page.evaluate(function() {
    1)находим элемент кнопки()
    2)генерируем событие нажатия()
    3)ждем подгрузки()
    4)собираем нужную информацию  с элементов по селекторам()
    return elements_array;
  });
console.log(elements_array);
});

I'm doing steps 1 and 2 from the page.evaluate function, but when I try to perform an asynchronous operation (step 3), console.log(elements_array) returns null because the function hasn't been executed yet.
How to solve the problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question