D
D
DIGIUS2019-02-26 13:32:16
JavaScript
DIGIUS, 2019-02-26 13:32:16

How to click on PhantomJS elements?

I wrote code on my knee to work with OCR Yandex Translate in the PhantomJS environment. To translate the text of a picture from English into Russian. the code is the following:

var webPage = require('webpage');
var page = webPage.create();
var page = require('webpage').create();
if(exist('photo.jpg')){
    remove('photo.jpg'); //удаление старых отладочных файлов
}
if(exist('example.html')){
    remove('example.html'); //удаление старых отладочных файлов
}
page.viewportSize = {
  width: 480,
  height: 800
};
page.open('https://translate.yandex.ru/ocr', function() {
page.uploadFile('input[id="fileInput"]', 'image.jpg'); //загрузка файла на сервер
page.evaluate(
        function( mouseclick_fn ) {
            var element = jQuery( 'rect' )[0];
            setTimeout(function(){mouseclick_fn( element );},5000);
        },
       mouseclick
   );
   setTimeout(function(){page.render('photo.jpg');file('example.html',page.content);phantom.exit();},10000); //для отладки
});
page.onLoadFinished = function() {
  console.log("load finished");
};
function remove(file){
var fs = require('fs');
fs.remove(file);
}
function exist(file){
var fs = require('fs');
if (fs.exists(file))
return true;
else
return false;
}
function file(filename,content){
var fs = require('fs');
fs.write(filename, content, 'w');
}
function mouseclick( element ) {
    var event = document.createEvent( 'MouseEvents' );
    event.initMouseEvent( 'click', true, true, window, 1, 0, 0 );
    element.dispatchEvent( event );
}

He does not want to click on rect even when commanding the chrome console . I think to emit a mouse click, but all the examples that I took from the Internet did not work either. How to make this piece of code work? Thanks in advance. document.getElementsByTagName("rect")[0].click

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DIGIUS, 2019-03-19
@DIGIUS

The answer was found when solving the problem in a different way.
At the time of the current version of the translator (03/19/2019), after uploading the image, you do not need to click on the translation results, they come immediately after uploading the image to the server in the 83 header (taking into account the numbering) of the server response ( Json) .
At this point, they only need to be received and parsed.
Unfortunately, phantomjs in its current form, no matter how I try to read it, does not want to.

A
Alexander Drozdov, 2019-02-26
@bagzon

Well, firstly, it is no longer supported and outdated)
Try it better https://github.com/GoogleChrome/puppeteer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question