【问题标题】:Can't click on element with Horseman不能用 Horseman 点击元素
【发布时间】:2017-06-17 18:45:17
【问题描述】:

我正在构建一个能够允许用户将 Google 快讯创建为 RSS 提要的应用。

刚开始使用 Horseman/PhantomJS,我现在在网页上遇到的问题是单击菜单以在“交付至”上显示选项:

通过浏览器访问此页面并单击您的电子邮件将出现的位置,将出现一个带有两个选项的小div,供您选择“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>

【问题讨论】:

    标签: javascript html node.js phantomjs node-horseman


    【解决方案1】:

    我认为问题在于它无法点击给定的类,尝试使用不同的 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"});
        }
    });
    

    希望这会有所帮助。

    【讨论】:

    • 不幸的是它没有。更难的是,我发现点击选项时显示的div 实际上是通过 JS 嵌入的。换句话说,在点击之前它不存在,我可能很容易点击,甚至隐藏。
    【解决方案2】:

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

    在您的脚本中单击不起作用,因为该下拉菜单没有监听点击:

    相反,您可以模拟鼠标按下:

    // 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);
    })
    

    【讨论】:

    • 是的,这似乎是问题所在,此类元素没有click 事件。我不知道我可以检查它,谢谢。根据渲染的图像,它起作用了,我看到应该出现的选项,虽然它没有对齐,但我相信它很好。非常感谢,@Vaviloff
    猜你喜欢
    • 1970-01-01
    • 2019-12-29
    • 2015-03-14
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多