Puppeteer - 按类删除元素

时间:2018-06-14 23:02:40

标签: javascript node.js puppeteer

我尝试按类名删除元素,但它不起作用。

这是我使用的代码:

await page.screenshot({path: 'pic.png'});   //for testing purposes
    let div_selector_to_remove= ".xj7.Kwh5n";
    var thingToRemove = document.querySelectorAll(div_selector_to_remove);
    var number_of_elements = thingToRemove.length;
    for (var i = 0; i < number_of_elements.length; i++) {
        thingToRemove[i].parentNode.removeChild(thingToRemove);
    }

浏览器加载并获得加载元素的屏幕截图。什么都没发生。元素仍然存在

2 个答案:

答案 0 :(得分:7)

如果不在E: Unable to locate package postgresql-9.6-postgis-2.3 E: Couldn't find any package by glob 'postgresql-9.6-postgis-2.3' E: Couldn't find any package by regex 'postgresql-9.6-postgis-2.3' 内写一下document.querySelector,你怎么能运行? 如果有可能知道。否则这是我的答案版本

page.evaluate

答案 1 :(得分:1)

首先,number_of_elements是数字。

但你打电话给number_of_elements.length

接下来,thingToRemove[i].parentNode.removeChild(thingToRemove)thingToRemove[i].parentNodethingToRemove[i]的父,而不是thingToRemove

因此您无法从thingToRemove删除thingToRemove[i].parentNode

我认为这段代码可能很有用。

await page.screenshot({path: 'pic.png'});   //for testing purposes
let div_selector_to_remove= ".xj7.Kwh5n";
var thingToRemove = document.querySelectorAll(div_selector_to_remove);
var number_of_elements = thingToRemove.length;
for (var i = 0; i < number_of_elements; i++) {
    thingToRemove[i].parentNode.removeChild(thingToRemove[i]);
}