无法使用 Horseman 单击元素

Can't click on element with Horseman

我正在构建一个应用程序,它能够允许用户创建 Google 警报作为 RSS 提要。

刚开始使用 Horseman/PhantomJS,我现在在网页上遇到的问题是单击菜单以显示 "Deliver to" 上的选项:

通过浏览器访问此页面并单击您的电子邮件将出现的位置,将出现一个带有两个选项的小 div,供您 select "RSS Feed"。通过 Horseman,我找不到点击它的方法。我在click()之后截图来看了,但是什么也没有。我的代码:

horseman
  .open('https://google.com/alerts')
  .type('input[type="text"]', this.name)
  .click('span[class="show_options"]')
  .screenshot('/home/gabriel/Desktop/ga-named.png')
  .wait(2000)
  .click('.delivery_select > div.jfk-select')
  .screenshot('/home/gabriel/Desktop/ga-deliver-to.png')
  .close()

只有.click('.delivery_select > div.jfk-select')不工作。在该特定区域的 HTML 下方:

<div class="delivery_select">
  <div class="goog-inline-block goog-flat-menu-button jfk-select" role="listbox" aria-expanded="false" tabindex="0" aria-haspopup="true" style="user-select: none;" aria-activedescendant=":8m">
    <div class="goog-inline-block goog-flat-menu-button-caption" id=":8m" role="option" aria-setsize="2" aria-posinset="1">myemail@foo.com</div>
    <div class="goog-inline-block goog-flat-menu-button-dropdown" aria-hidden="true">&nbsp;
    </div>
  </div>
</div>

我认为问题在于它无法点击给定的 class,尝试使用不同的 ID。 查看我如何使用 horseman 登录。

router.post('/login', function (req, res) {
    //default urls is http://144.76.34.244:8080/magento/1.9/web/customer/account/login//
    var url_ = req.body.url;
    var username = req.body.username;
    var password = req.body.password;
    if (url_.length > 0 && username.length > 0 && password.length > 0) {
        horseman
                .userAgent('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0')
                .open(url_)
                .type('input[name="login[username]"]', username)
                .type('input[name="login[password]"]', password)
                .click('[name="send"]')
                .wait(5000)
                .url()
                .then(function (url) {
                    if (url == config.URL) {
                        res.json({status: "200", msg: 'login successfull', url: url});
                    } else {
                        res.json({status: "200", msg: "login failed"});
                    }
                })
                .screenshot('big.png')
                .close();
    } else {
        res.json({status: "500", msg: "invalid fields"});
    }
});

希望对您有所帮助。

我从未使用过 Horseman,但您肯定能够自己实施该解决方案。

在您的脚本中点击不起作用,因为 drop-down 没有监听点击:

相反,您可以模拟 mousedown:

// Click "Deliver to" dropdown
.evaluate(function(){
    var event = document.createEvent ('MouseEvents'); 
    event.initEvent ("mousedown", true, true);     

    document.querySelector('.delivery_select > div.jfk-select').dispatchEvent(event);
})