简体   繁体   English

单击远程Div上的按钮。 卡斯珀

[英]Click button on remote Div | CasperJS

I am trying to automate a process using CasperJS which requires clicking a button in a remote Div. 我正在尝试使用CasperJS自动化过程,这需要单击远程Div中的按钮。 My main page will have only mentioning of the remote div and the button is present inside the remote div. 我的主页只会提到远程div,并且该按钮位于远程div内。

main page: 主页:

<div id= "main_container"></div>

remote div: 远端div:

<div id="main_container">
    <div id="agenda_items">
        <button type="button">Click Me!</button>
    </div>
</div>

I am trying to click the Click Me button from the casperjs script that runs from main page. 我试图从从主页运行的casperjs脚本中单击“单击我”按钮。 I was able to do it when the page used iframe by using this.switchToChildFrame / this.withFrame . 当页面通过使用this.switchToChildFrame / this.withFrame使用iframe时,我能够做到这this.withFrame Is there any similar way with Divs also? Divs也有类似的方法吗? If it's not possible with CasperJS, is there any other possibility? 如果CasperJS无法实现,还有其他可能性吗? Please advise. 请指教。 Thanks 谢谢

If I'm understanding your question correctly then you have two options: 如果我正确理解了您的问题,那么您有两种选择:

  1. Use jQuery inside of an evaluate method to click the button you're referring to. 在评估方法中使用jQuery单击您所指的按钮。

    Maybe something like this: 也许是这样的:

     casper.evaluate(function() { $('button:contains("Click Me")').click(); } 
  2. Use the clickLabel method that's built into Casper 使用Casper内置的clickLabel方法

     casper.then(function() { this.clickLabel('Click Me!', button); }); 

You don't need to "switch" to a div. 您无需“切换”到div。 Simply determine an appropriate CSS selector or XPath expression that matches the button you're trying to click. 只需确定与您要单击的按钮匹配的适当的CSS选择器或XPath表达式即可。 Then you can use casper.click 然后您可以使用casper.click

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM