0

出于某种原因,querySelector 和 get element by class 在存在的元素上返回 null。

PhantomJS/SlimerJS

page.open('file:///Users/yeahsame/Desktop/index.html', function(status)
{
    console.log("Starting execution");
    document.querySelector("input")[0].value = "not working whatsoever";

    phantom.exit();
});

HTML:

<!doctype html>
<body>
    <input class="form-control email input-lg"></input>
    <button class="btn" onclick="location.href='notexist.html'">submit</button>
</body>

在 slimerjs 中运行返回“document.querySelector(...) is null”

4

1 回答 1

1

PhantomJS/SlimerJS 有两个上下文。内页 (DOM) 上下文只能通过沙盒page.evaluate()功能访问。在它之外存在一个document对象,但它无权访问页面 DOM。

page.open('file:///Users/yeahsame/Desktop/index.html', function(status)
{
    console.log("Starting execution");
    page.evaluate(function(selector, value){
        document.querySelector(selector)[0].value = value;
    }, "input", "not working whatsoever");

    page.render("screenshot.png");
    phantom.exit();
});

内部的代码page.evaluate()无法访问外部定义的变量,因此必须显式传入这些值。

于 2015-08-27T15:58:17.947 回答