Answer the question
In order to leave comments, you need to log in
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 );
}
document.getElementsByTagName("rect")[0].click
Answer the question
In order to leave comments, you need to log in
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.
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 questionAsk a Question
731 491 924 answers to any question