【问题标题】:CasperJS click is leads to error even when the element exists即使元素存在,CasperJS click 也会导致错误
【发布时间】:2015-11-02 15:48:32
【问题描述】:

我正在尝试单击从当前页面检索到的注销按钮。我成功获得了注销链接的id。但是当我点击它时,出现错误

无法在不存在的选择器上调度 mousedown 事件

function getLogoutId()
{
    var logoutid = "";
    $(document).find("a").each(function(key,value){

        var id = $(this).attr("id");

        if(id.toLowerCase().indexOf("logout") > -1)
        {
            __utils__.echo("insidelogout" + id);
            logoutid = id;
        }
    });

    return logoutid;
}


var logoutid = this.evaluate(getLogoutId);
fs.write("logout.txt", this.getHTML(), 'w');
this.thenClick("#"+logoutid, function(){});

我已将 html 内容写入一个文件,我在其中检查了id,它就在那里。有问题的 id 属性如下所示:

et-ef-content-ftf-flowHeader-logoutAction

【问题讨论】:

  • id 包含破折号et-ef-content-ftf-flowHeader-logoutAction

标签: javascript css-selectors click casperjs


【解决方案1】:

除了奇怪地使用 jQuery 之外,我认为您的代码没有任何问题。

您可以尝试其他 CSS 选择器进行点击:

casper.thenClick("[id*='logout']"); // anywhere

casper.thenClick("[id$='logoutAction']"); // ending

casper.thenClick("[id|='logoutAction']"); // dash-separated

可能是显示代码后面的代码存在问题。您可以尝试将thenClick 更改为click

【讨论】:

    【解决方案2】:

    您是否尝试过只使用this.click("#"+logoutid);? 你也考虑过使用jQuery来点击按钮吗?像这样的东西......(首先为你的 id 创建一个变量,这样你就可以传递给 jQuery)。

    var id = "#"+logoutid;
    this.evaluate(function(id){
          jQuery(id).click();
    },id);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-11
      • 2014-12-03
      • 1970-01-01
      • 2018-11-24
      • 2021-06-17
      • 2021-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多