【问题标题】:NightmareJS .click() method - selector issueNightmareJS .click() 方法 - 选择器问题
【发布时间】:2017-01-28 16:41:43
【问题描述】:

我无法让 nightmareJS 点击搜索按钮,尽管 .type() 效果很好。 可能是什么问题?

.type('form[action="/search/web/direct_search.php"] [class=_1frb]', string)
.click('form[action="/search/web/direct_search.php"] [type=submit]')
//.click('#js_x > form > button')
//.click('button[class="_42ft _4jy0 _4w98 _4jy3 _517h _51sy"]')

开发者工具控制台找到按钮:

 document.querySelector('form[action="/search/web/direct_search.php"] [type=submit]')
 <button value=​"1" class=​"_42ft _4jy0 _4w98 _4jy3 _517h _51sy" aria-label=​"Search" tabindex=​"-1" data-testid=​"facebar_search_button" type=​"submit">​…​</button>​

稍后编辑:

你去吧,我的朋友,我已经删除了导入并将所有内容放在一个文件中,我已经使用 cookie 登录,所以这是复制问题的完整代码:

var Nightmare = require('nightmare');
var nightmare = Nightmare({
    show: true
});
var string = "TEST";
var search = function(string) {
    return function(nightmare) {
        nightmare
            .goto('http://www.facebook.com')
            .wait('form[action="/search/web/direct_search.php"] [class=_1frb]')
            .type('form[action="/search/web/direct_search.php"] [class=_1frb]', string)
            .click('form[action="/search/web/direct_search.php"] [type=submit]')
            // .click('#js_x > form > button')
            // .click('button[class="_42ft _4jy0 _4w98 _4jy3 _517h _51sy"]')
            .wait(2000)
            .screenshot('after.png')
            .then(function() {
                return nightmare
                    .wait();
            })
    };
};
nightmare
    .useragent('...')
    .use(search(string))

【问题讨论】:

  • 欢迎来到 Stack Overflow!看来您需要学习使用调试器。请帮助自己一些complementary debugging techniques。如果之后仍有问题,请随时回来提供更多详细信息。
  • @JoeC 我已经相应地更新了我的帖子。
  • 我将假设您没有阅读我关于如何调试的帖子。
  • @JoeC 我已经阅读了你的帖子。你可以看到我已经展示了控制台使用相同的选择器找到了按钮。这是一个调试操作。

标签: javascript events dom css-selectors click


【解决方案1】:

Nightmare 的 .type() 发出键盘事件。您可以使用 unicode 值“按下”返回键,如下所示:.type('selector', '\u000d')。我在下面提供的示例应该可以正常工作。

var Nightmare = require('nightmare');
var nightmare = Nightmare({
    show: true
});

nightmare
    .goto("http://www.facebook.com")
    .wait('form[action="/search/web/direct_search.php"] [class=_1frb]')
    .type('form[action="/search/web/direct_search.php"] [class=_1frb]', "test\u000d")
    .catch(function(error) {
        console.log(error)
    })

通常在 Facebook 上搜索时,建议菜单会在您输入查询时打开。按下搜索按钮应立即搜索搜索。但是,如果您观看 Nightmare 的执行,您会看到建议菜单在输入时出现故障并关闭(这是电子浏览器的错误)。所以我相信你必须按两次搜索按钮;一次打开菜单,然后再次实际搜索。

无论哪种方式,只发送一个返回键事件都更容易更好。

【讨论】:

    猜你喜欢
    • 2017-08-16
    • 2017-01-09
    • 2015-02-07
    • 1970-01-01
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2011-04-04
    相关资源
    最近更新 更多