如何使用php-phantomjs在无头chrome中执行Javascript?

I'm trying to run a headless browser and run JS scripts (a bot) within it using but want to run it using php. Searching on Google, I found a implementation / wrapper of PhantomJS as php-phantomjs. Please bear with me, I'm very new to this stuff.

Here what I'm trying to do is to take screenshot of alert window (this is not necessary, but just to test if JS is executed and then the screenshot is taken.)

Here is my code:

// file: app.php

$client = PhantomJsClient::getInstance();
$client->isLazy();

$location = APP_PATH . '/partials/';

$serviceContainer = ServiceContainer::getInstance();
$procedureLoader = $serviceContainer->get('procedure_loader_factory')->createProcedureLoader($location);

$client->getProcedureLoader()->addLoader($procedureLoader);

$request  = $client->getMessageFactory()->createCaptureRequest();
$response = $client->getMessageFactory()->createResponse();

$request->setViewportSize(1366, 768);
$request->setHeaders([
    'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'
]);

$request->setTimeout(5000);
$request->setUrl('https://www.google.co.in');

$request->setOutputFile(APP_PATH . '/screen.jpg');

$client->send($request, $response);

Tried two custom scripts as given in the list here: http://jonnnnyw.github.io/php-phantomjs/4.0/4-custom-scripts/

// file: page_on_resource_received.partial, page_open.partial

alert("Hello");

OUTPUT: It just shows the page, not the alert window.

I repeat, Its not about taking screenshot, that's just to be sure that JS is executing. I just want to execute my JS scripts (better say bots) like:

var form = document.getElementById('form');
var event = new Event('submit');
form.dispatchEvent(event);

or maybe using jQuery and then return the output of that page to php as response or so. So, if there is any other way to run bots using php in a headless browser please mention that in your answers or comments.

展开翻译

译文

我正在尝试运行无头浏览器并在其中运行JS脚本(机器人),但想要运行 它使用PHP。 在Google上搜索,我发现PhantomJS的实现/包装为php-phantomjs。 请耐心等待我,我对这些东西很新。</ p>

这里我要做的就是截取警报窗口的截图(这不是必需的,只是为了测试 如果执行JS然后截取屏幕截图。)</ p>

这是我的代码:</ p>

  // file:app.php \  n 
$ client = PhantomJsClient :: getInstance();
$ client-&gt; isLazy();

$ location = APP_PATH。 '/ partials'';

$ serviceContainer = ServiceContainer :: getInstance();
$ procedureLoader = $ serviceContainer-&gt; get('procedure_loader_factory') - &gt; createProcedureLoader($ location);

$ client-&gt; getProcedureLoader() - &gt; addLoader($ procedureLoader);

$ request = $ client-&gt; getMessageFactory() - &gt; createCaptureRequest();
$ response = $ client-&gt; getMessageFactory( ) - &gt; createResponse();

$ request-&gt; setViewportSize(1366,768);
$ request-&gt; setHeaders([
'User-Agent'=&gt;'Mozilla / 5.0(X11) ; Linux x86_64)AppleWebKit / 537.36(KHTML,如Gecko)Chrome / 57.0.2987.98 Safari / 537.36'
]);

$ request-&gt; setTimeout(5000);
$ request-&gt; setUrl( 'https://www.google.co.in');

$ request-&gt; setOutputFile(APP_PATH。'/ screen.jpg');
nnnnclient-> send($ request, $ response);
</ code> </ pre>

尝试了两个自定义脚本,如下表所示: http://jonnnnyw.github.io/php-phantomjs/4.0/4- custom-scripts / </ p>

  // file:page_on_resource_received.partial,page_open.partial 

alert(“Hello”);
</ code> < / pre>

输出:</ strong>它只显示页面,而不是警告窗口。</ p>

我再说一遍,它不是关于截屏, 这只是为了确保JS正在执行。
我只想执行我的JS脚本(更好的说是机器人),如:</ p>

  var form = document.getElementById('form'  ); 
var event = new Event('submit');
form.dispatchEvent(event);
</ code> </ pre>

或者可能使用jQuery然后返回该输出 页面到PHP作为响应左右。 因此,如果在无头浏览器中使用php运行机器人有任何其他方式,请在您的答案或评论中提及。</ p>
</ div>

1个回答

PhantomJS is headless, that is it doesn't have GUI. Therefore no window dialogs can be seen.

Try writing custom text to an element instead of alert, like

document.getElementById("#title").innerHTML = "It works!";

展开翻译

译文



PhantomJS是无头的,就是它没有GUI。 因此,无法看到窗口对话框。</ p>

尝试将自定义文本写入元素而不是警告,例如</ p>

  document.getElementById(“  #title“)。innerHTML =”它有效!“; 
</ code> </ pre>
</ div>

dongseshu0698
dongseshu0698 是的,我知道。 我尝试根据自己的需要进行一点修改,并在不在Google页面上的自定义HTML上进行测试。 你能不能给我一些你用来完成这个代码的代码片段?
2 年多之前 回复
dougou8552
dougou8552 不要按原样使用它,Google页面上没有#title元素。
2 年多之前 回复
dpnv33177
dpnv33177 我尝试使用您的代码,但没有更改输出。 我在程序中做错了吗?
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问