javascript - 带有 R : webshot. js 的 Webshot 返回失败值 1

标签 javascript jquery html r phantomjs

我想使用 webshot 将来自特定站点的一些 Web 元素导出到 png 文件R图书馆。

首先,安装和加载库:

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

我正在测试 webshot() www.google.es 网址。它运作良好:
webshot("https://www.google.es/","google.png", selector="#hplogo")

enter image description here

但如果我想将搜索引擎元素导出为 PNG 图像:

enter image description here
我写了以下代码:
webshot("https://www.google.es/","google.png", selector=".tsf-p")

怎么了?
> 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 

最佳答案

可能之前打开了一个网页(接受 cookie 的页面)。
因此,有必要模拟点击以接受 cookie。
然后类(class)好像变了:div.o3j99.ikrT4e.om7nvf
enter image description here
这是一个适合我的代码:

    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 允许为镜头选择矩形

关于javascript - 带有 R : webshot. js 的 Webshot 返回失败值 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44006186/

相关文章:

javascript - 使用加号和减号控件输入以按字母顺序增加值

php - 即使数据库中不存在空值,JSON 响应中也会返回空值

html - 增加连续的 div 的高度?

javascript - 如何在HTML中使用 `onlick attribute`调用jquery插件函数

javascript - 手机上的菜单不会消失

javascript - 在 HTML 中访问 SVG

javascript - Ng Bootstrap 轮播全屏

jquery - 使用外部链接自动加载页面 + ajax 调用(使用 Jquery)

html - 如何使绝对定位的元素与另一个元素具有相同的大小

javascript - 在 AngularJS 中创建 Controller 时出现问题