Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webshot with R: webshot.js returned failure value 1

I want to export to png files some web elements from a specific site with webshot R library.

First, installing and loading libraries:

install.packages("webshot",dependencies = TRUE)
library(webshot)
webshot::install_phantomjs()

I'm testing webshot() with www.google.es URL. It works well:

webshot("https://www.google.es/","google.png", selector="#hplogo")

enter image description here

But If I want to export to PNG image the search engine element:

enter image description here I write the following code:

webshot("https://www.google.es/","google.png", selector=".tsf-p")

What is wrong?

> webshot("https://www.google.es/","google.png", selector=".tsf-p")
PHANTOM ERROR: CasperError: No element matching selector found: .tsf-p
TRACE:
 -> phantomjs://platform/casper.js: 1066 (in function getElementBounds)
 -> phantomjs://code/webshot.js: 137
 -> undefined: 0 (in function map)
 -> phantomjs://code/webshot.js: 136 (in function findClipRect)
 -> phantomjs://code/webshot.js: 85
 -> phantomjs://platform/casper.js: 2188 (in function _check)
Error in webshot("https://www.google.es/", "google.png", selector = ".tsf-p") : 
  webshot.js returned failure value: 1
In addition: Warning message:
running command '"C:\Users\Mario Martínez\AppData\Roaming/PhantomJS/phantomjs.exe" "C:/Users/Mario Martínez/Documents/R/win-library/3.3/webshot/webshot.js" "[{\"url\":\"https://www.google.es/\",\"file\":\"google.png\",\"vwidth\":992,\"vheight\":744,\"selector\":\".tsf-p\",\"delay\":0.2,\"zoom\":1}]"' had status 1 
like image 692
Mario M. Avatar asked May 16 '17 15:05

Mario M.


1 Answers

It is possible that there is a web page that opens before (a page to accept cookies). It is therefore necessary to simulate a click to accept cookies.

Then the class seems to have changed:div.o3j99.ikrT4e.om7nvf enter image description here

Here is a code that works for me :

    webshot("https://www.google.com/", file = "Test.png", 
    eval = "casper.then(function() {
    this.click('#L2AGLb'); // click cookies accept
    this.sendKeys('input.gLFyf.gsfi', 'test_text_research', {keepFocus: true}); // add text in search bar
    this.wait(1000);
    });",
    cliprect = c(200, 150, 800, 300)) 

cliprect allow to choice rectangle for the shot

like image 138
Pierre Rollan Avatar answered Dec 04 '22 23:12

Pierre Rollan