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")
But If I want to export to PNG image the search engine element:
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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With